Home > Java > Javagetting Started > body text

What is an exception that is not thrown in java

王林
Release: 2019-11-21 16:59:56
Original
2917 people have browsed it

What is an exception that is not thrown in java

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template