Will the code in finally be executed? There is return in try, is finally still executed?
What is this question trying to investigate?
In-depth understanding of the Java language to avoid writing "problem" code during development
Knowledge points of inspection
JVM execution process
How should candidates answer
The description of finally in the official Java documentation is as follows:
The `finally` block *always* executes when the `try` block exits.
The general meaning is: the content in the finally code block will definitely be executed.
There is also a clear statement in the JVM specification
If the try clause executes a return, the compiled code does the following:
1. Saves the return value (if any) in a local variable.
2. Executes a jsr to the code for the finally clause.
3. Upon return from the finally clause, returns the value saved in the local variable.
It means that if there is a return in the try, the return value in the try will be stored in the local variable table of the stack frame, then the finally statement block will be executed, and finally the return value will be retrieved from the local variable table and returned. In addition, when there is a return in both try and finally, the return of try will be ignored and the return of finally will be used.
Special case
Under normal circumstances, the code in finally will definitely be executed, but if we set the thread executing the try-catch-finally code block as a daemon thread, or call to end the current virtual machine before fianlly, then finally will not be System.exit
executed :
try{
System.exit(0);
}catch (Exception e){
}finally {
}
Thread t1 = new Thread(){
@Override
public void run(){
//try-catch-finally
}
};
t1.setDaemon(true);//设置为守护进程
t1.start();
2.8 In the Java exception mechanism, the difference between exception Exception and error Error
What is this question trying to investigate?
When you need to customize exceptions during development, should you choose to define Excption or Error? What does the written code trigger Excption or Error represent?
Knowledge points of inspection
Java exception mechanism
How should candidates answer
Throwable
There is a throwable class in Java Throwable
with two important subclasses, one is Error and the other is Exception.
[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-oCdRO3Fe-1686365970528)(images\abnormal.png)]
Error
It is an error that the program cannot handle, indicating a serious problem in the program. For example, Java virtual machine running error (Virtual MachineError), when the JVM no longer has the memory resources needed to continue the operation, OutOfMemoryError will appear and so on. When these errors occur, the JVM will generally choose to terminate the thread. These errors cannot be checked, they are outside the control and processing capabilities of the program, and most of them are not allowed to occur when the program is running.
Exception
It is an exception that the program can handle. And Exception is divided into runtime exception (RuntimeException) and non-runtime exception.
-
Abnormal operation
Runtime exceptions, also known as unchecked exceptions . The so-called unchecked means that Java will not tell us that there is an exception when compiling and checking, and it needs to be exposed at runtime, such as subscript out of bounds, null pointer exception, etc.
-
non-runtime exception
Exceptions other than RuntimeException are collectively referred to as non- runtime exceptions , such as IOException and SQLException, which are exceptions that must be handled ( check exceptions ). If they are not handled (throw to the upper layer or try-catch), the program cannot be compiled.