Exception Classes in java.lang:
Unchecked Exceptions:
Examples of unchecked exceptions:
ArithmeticException: calculation error such as division by zero.
NullPointerException: use of null reference.
ClassCastException: Invalid class conversion attempt.
NumberFormatException: error converting String to a number.
Checked Exceptions:
They need to be listed in throws if the method can throw them.
They are mandatory for exceptions that the compiler requires handling, as they cannot be ignored.
Examples of checked exceptions:
ClassNotFoundException: class not found.
IllegalAccessException: access denied to a class.
InterruptedException: interruption of a thread by another thread.
NoSuchMethodException: requested method does not exist.
Chained Exceptions:
Introduced in Java 1.4 to specify one exception as the cause of another.
They allow you to associate an original exception with the generated exception, useful in scenarios with layers of errors.
Constructors for chained exceptions:
Throwable(Throwable cause): defines the causing exception.
Throwable(String msg, Throwable cause): allows you to add a descriptive message.
Methods:
getCause(): returns the cause of the current exception.
initCause(Throwable cause): defines the cause after the exception was created.
Considerations about Chained Exceptions:
Useful when knowledge of the root cause helps to understand the error.
Not every program needs to use chained exceptions, but they offer an elegant solution for complex scenarios.
The above is the detailed content of Java Language Internal Exceptions. For more information, please follow other related articles on the PHP Chinese website!