Checked and unchecked exceptions

        The so-called checked exception refers to the exception that is forced to be checked at compile time. This exception needs to be displayed through
Try/catch to capture, or throw out through throws, otherwise the slave program cannot be compiled.

 

Non-checked exceptions indicate exceptions that do not need to be checked by the compiler, and such exceptions do not need to be displayed to catch.
(As shown in the figure) In Java, all exceptions are inherited from java.lang.Throwable class, Throwable
There are two direct subclasses, Error and Exception.
Error is used to indicate the underlying program or hardware-related errors, which have nothing to do with the program itself, such as the common
OOM exception. This kind of exception has nothing to do with the program itself, so it does not need to be checked and is an unchecked exception.
Exception represents an exception in the program, which may be caused by an imprecise program, such as
NullPointerException。
Exception derives RuntimeException and other exceptions, where RuntimeException runs
Runtime exceptions are also unchecked exceptions.
Therefore, except for Error, RuntimeException and derived classes, other exceptions are checked exceptions.
Such as IOException, SQLException.

 

The reason why some mandatory checking exceptions are designed in Java, I think the main reason is to consider the correctness of the program,
stability and reliability.
Such as database exceptions, file reading exceptions, these exceptions cannot be expected by the program in advance, but once a problem occurs
If there is a problem, it will cause resources to be occupied and cause problems in the program.
So we need to actively capture these exceptions. Once there is a problem, we can deal with it accordingly, such as closing
Database connection, release of file stream, etc.

Guess you like

Origin blog.csdn.net/weixin_55229531/article/details/131488625