What is the difference between finally and no fina

2019-06-17 03:57发布

问题:

What is the difference between

try {
     // action A
}
catch(Exception e) {
     // action B
}
finally {
     // action C
}

and

try {
     // action A
}
catch(Exception e) {
     // action B
}
// action C

I have read that you can return from inside a catch block and still have the finally block execute. Are there any other differences?

回答1:

Things that happen within the finally block are guaranteed to occur no matter what happens in the try-catch-block. If an exception happens that is not encapsulated by Exception (e.g., extends Throwable, such as various Errors), then it still runs the finally block.

One thing to be aware of: if, within the finally block, a RuntimeException is thrown, or another Exception escapes from within it, then the rest of the finally block will not execute. Also, as Lord Torgamus pointed out, it is contingent on the JVM running. In addition, and probably obviously, it is also contingent on the thread not being stopped.



回答2:

Most of the existing answers contain pieces of the right answer, but none are quite spot-on.

The finally block is always guaranteed to be reached after the try and potentially catch blocks if the JVM does not shut down beforehand. However, if a bit of code inside the finally block shuts down the JVM, or throws an exception its own, the end of the block might not be reached.

Per the Sun Certified Programmer for Java 6 Study Guide:

  • The only exception to the finally-will-always-be-called rule is that a finally will not be invoked if the JVM shuts down.

  • Just because finally is invoked does not mean it will complete.

The final word is, as always, the Java Language Specification. The behavior of finally is explained exhaustively in §14.20.2 Execution of try-catch-finally.

As an extra note: you're right that a return in the try won't stop finally from running. In fact, finally is entered immediately after the return is encountered, before it executes.



回答3:

Better look at this example, resources are allways closed even if I return or propagate the Excpetion up.

try {
 //action A
 return 0;
} catch (Exception e){
 //action C
 throw new Excpetion("Probleme here",e)
} finnaly {
 //action C
 resources.close();
}

If action A and be are as primitve as int a = 0, then there is no difference, but in more complicated situations like this, finnaly block have it's usage



回答4:

from the Sun Tutorials

Note: If the JVM exits while the try or catch code is being executed, then the finally block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block will not execute even though the application as a whole continues.

I don't know of any other ways the finally block wouldn't execute...



回答5:

Code inside the finally block is guaranteed to execute should the JVM continue running.

This means that even if what is in action B throws another new exception or doesn't catch the exception in action A or a return is called, the code in action C will run.



回答6:

A try block is used for exception handling. If there any case/code that will throw exception, e.g., if we divide a number by Zero(0) than it will throw an exception and the process will be shutdown. In this case if we put our code in the try block than the exception is catch by the catch block and the process will not be terminated. And the finally block the guarantee of executing the code written there. Therefore, if we have to terminate/close the process even its execution is successful or not(e.g., in case of network programming we have to release the connection at last so that other devices can use that connection) we use the finally block.



标签: java finally