Java’s exception architecture

The root class of the Java exception system is Throwable, so when you write a throw in Java code that throws an exception, the object that follows must be an object of Throwable or a subclass of it.

Exception refers to exceptions that can be recovered, such as the common NullPointerException NullPointerException.

Error refers to fatal errors that cannot be recovered by means of program code, such as OutOfMemoryError.

Unchecked exceptions

In the diagram above, except for RuntimeException, Error, and its subclasses, which are all types of exceptions that are unchecked by the compiler.

Unchecked by the compiler, because errors can be programmatically controlled during a program’s run.

For example, the common NullPointerException null pointer exception and cross-border IndexOutOfBoundsException array subscripts is unusual, these can be used in advance if (xx! = null) and if (xxx.size() > I)

Or it’s completely beyond the control of the program,

Examples are OutOfMemoryError and StackOverflowError stack overflow exceptions, which are also examples of unchecked errors because they cannot be avoided by code level if.

Checked exceptions

Checked is checked by the compiler during compilation, and if the program does not catch or throw the exception up, it will fail to compile.

Common checked exceptions include a FileNotFoundException file that does not contain an exception. Because such exceptions are foreseen at the writing stage, such as the file is most likely not to exist, they must be thrown and handled by the program.

conclusion

Throwable Ancestor of any exceptions/errors that are checked exceptions.

Exception: Exceptions from which execution can be recovered are checked exceptions.

Unanticipated exceptions, such as null Pointers and array overbounds, are unchecked exceptions.

. Exception Except for RuntimeException and its subclasses, which are unchecked exceptions, all Exception classes are checked.

Error, a fatal problem that cannot be recovered from an Error, is also an unchecked exception.

In the development process, if some errors can be expected to throw exceptions, as far as possible to throw checked exceptions, such as that file, a certain data may not exist, it is necessary to remind the method caller, need to deal with this situation.

Use RuntimeException for unexpected exceptions, such as a value that must not be null, but the program determines that it is null and throws a RuntimeException.

The interview questions

What is checked/unchecked/runtime exception?

Checked Exception refers to all exceptions except Error, Runtime Exception, and subclasses,

Unchecked Exception refers to exceptions such as Error, Runtime Exception, and its subclasses,

Runtime Exception is an unchecked exception.

Try /catch/finally execution order

Try is used to contain the block of code that runs at the time,

Catch is used to catch exceptions that may occur while the code is running, followed by the second step

When an error occurs at a certain point in a block of code, the following code will not execute,

Instead, it jumps to a block of code for a catch, which is executed from top to bottom with the first catch that can catch the current exception.

Finally is a program where the code will eventually execute whether or not an exception occurs, so it is the third step.

What happens to return data in finally

Since finally is executed no matter what, the return in finally, or overrides the return elsewhere, will eventually return the most. In the figure, finally returns 2.

Throws and throws

Throw new XXX() throws an exception to terminate the current program if it encounters an incorrect value or result.

Throws throws on the method signature to indicate what exceptions the method will throw, tell the caller the exceptions that may be generated by calling the method, and let the caller handle them accordingly.

The difference between final, finally and Finalize

Final is used to modify classes, methods, and variables, on which the class cannot be inherited, on which the method cannot be overridden, and on which the variable reference cannot be changed.

Finally is used in try statements, meaning that the code contained in finally must be executed, with or without exceptions.

Finalize is a way to all the objects in the object before being recycled, will be garbage collector calls, but will only call time, generally can save the current will be recycled in the method of object, such as the use of a variable reference current object, but this approach is not desirable, because the garbage collector will not guarantee that the method is carried out, The object may be reclaimed during assignment,

This method is similar to C++ destructors, but is unstable and officially not recommended, but is a compromise made for historical reasons to make C++ programmers more comfortable with Java.