RuntimeException and its subclasses and errors (Error) do not have to be caught or thrown.
Java exceptions Throwable
are divided into two categories: Error
and Exception
. The Error class is an error that cannot be handled by the program itself.
Exception is divided into two categories: IOException
(non-runtime exception) and RuntimeException
(runtime exception). RuntimeException is to be avoided during program design. .
Except RuntimeException and its subclasses, other Exception classes and their subclasses are checkable exceptions. The compiler requires forced handling of this kind of exception, either try-cathch or throw it after the method name.
Uncheckable exceptions (exceptions that the compiler does not require forced handling): include runtime exceptions (RuntimeException
and its subclasses) and errors (Error
).
Exception This kind of exception is divided into two categories: runtime exception and non-runtime exception (compilation exception).
Runtime exceptions: They are all exceptions of the RuntimeException class and its subclasses, such as NullPointerException
(null pointer exception), IndexOutOfBoundsException
(subscript out-of-bounds exception) Etc. These exceptions are unchecked exceptions. You can choose to capture them in the program or not handle them.
These exceptions are generally caused by program logic errors, and the program should try to avoid the occurrence of such exceptions from a logical perspective.
The characteristic of runtime exceptions is that the Java compiler will not check it. That is to say, when this type of exception may occur in the program, it is useless even if it is not captured with a try-catch statement. The throws clause declares that it will be thrown, and it will compile.
Non-runtime exception (compilation exception): From the perspective of program syntax, it is an exception that must be handled. If it is not handled, the program will not be compiled. Such as IOException
, SQLException
, etc., as well as user-defined Exceptions, but generally no checked exceptions are customized.
In fact: RuntimeException, its subclasses and errors (Error) do not have to be caught or thrown.
Recommended tutorial: java introductory tutorial
The above is the detailed content of What is an exception that is not thrown in java. For more information, please follow other related articles on the PHP Chinese website!