Two features of finally:
Finally in Java is important for languages that do not have garbage collection and automatic invocation mechanisms for destructors. It allows the programmer to ensure that:
1. The finally clause is always executed, regardless of whether the exception is thrown. This feature can be used to solve a problem where Java exceptions do not allow us to return to the place where the exception was thrown, what should we do? Put the try block in the loop and establish a condition that "the program must be reached before it continues to execute". You can also add a device such as a static type counter, so that the loop can try a certain number of times before giving up, which will make the program more robust.
2. No matter what happens in the try block, memory is always released. But Java has a garbage collection mechanism, so memory deallocation is no longer a problem, and Java has no destructor to call. So, under what circumstances can Java use finally?
Use a finally clause when you want to restore resources other than memory to their initial state. Resources that need to be cleaned up include open files or network connections, graphics on the screen, and even a switch from the outside world.
Two situations that can cause an exception to be lost:
1, however, if you use the FINALLY clause in some special way, you can cause an exception to be lost:
PackageCom.test.exception.lost; Public classVeryimportantexceptionextendsexception{ PublicString toString () {return"A Very Important exception!"; }} PackageCom.test.exception.lost; Public classHohumexceptionextendsException { PublicString toString () {return"A Trivial exception"; }} PackageCom.test.exception.lost; Public classLostmessage {voidF ()throwsveryimportantexception {Throw Newveryimportantexception (); } voidDispose ()throwshohumexception {Throw Newhohumexception (); } Public Static voidMain (string[] args) {Try{lostmessage lm=NewLostmessage (); Try{lm.f (); } finally{lm.dispose (); } } Catch(Exception e) {System.out.println (e); } }}
As you can see in the output, Veryimportantexception is missing and replaced by the hohumexception in the finally clause. This is a pretty serious flaw, and C + + sees a bad programming error in the case of "throwing the next exception before it's done". This problem may be fixed in a future version of Java (JDK1.7 here).
2. A simpler exception loss scenario is returned from the finally clause:
package Com.test.exception.lost; public class Exceptionsilencer {@SuppressWarnings ( "finally" public static void main (string[] args) { try { throw new RuntimeException (); finally { return ; } }}
If you run this program, you will see that an exception is thrown in time and no output is generated. (This does not stick to the graph of running results, there will be no output information in the console)
Summarize:
Finally, it is of great use to ensure the correctness of the program, but it is important to avoid the following two kinds of situations that can cause abnormal loss (at least in JDK1.7).
1. Throws an exception in the finally clause;
2. Returns (return) in the finally clause.
Java exception-Unexpected loss may occur &finally