When exceptions are thrown, the usual method of execution will be a steep non-linear steering. Depending on how the method is encoded, an exception can even cause the method to return prematurely. This is a problem in some ways. For example, if a method opens a file entry and closes and then exits, you do not want the code to close the file to be bypassed by the exception handling mechanism. The finally keyword is designed to handle this kind of accident.
Finally, create a block of code. The code block executes before another try/catch occurs after one try/catch block is complete. The finally block executes regardless of whether an exception is thrown. If the exception is thrown, finally even the catch clause that does not match the exception is executed. A method will return from a try/catch block to the calling program at any time, after an uncaught exception or an unambiguous return statement, and the finally clause will still execute before the method returns. This is useful in closing the file handle and releasing any other resources that are allocated at the beginning of the method. The finally clause is optional and can or may not be. Each try statement, however, requires at least one catch or finally clause.
The following example shows 3 different exit methods. Each of them executes a finally clause:
1 //demonstrate finally.2 classFinallydemo {3 //Through an exception out of the method.4 Static voidProca () {5 Try {6System.out.println ("Inside Proca");7 Throw NewRuntimeException ("Demo");8}finally {9System.out.println ("Proca ' s finally");Ten } One } A - //Return from within a try block. - Static voidPROCB () { the Try { -System.out.println ("Inside PROCB"); - return; -}finally { +System.out.println ("PROCB ' s finally"); - } + } A //Execute a try block normally. at Static voidProcc () { - Try { -System.out.println ("Inside PROCC"); -}finally { -System.out.println ("Procc ' s finally"); - } in } - to Public Static voidMain (String args[]) { + Try { - Proca (); the}Catch(Exception e) { *SYSTEM.OUT.PRINTLN ("Exception caught"); $ }Panax Notoginseng PROCB (); - PROCC (); the } + } A //demonstrate finally. the classFinallydemo { + //Through an exception out of the method. - Static voidProca () { $ Try { $System.out.println ("Inside Proca"); - Throw NewRuntimeException ("Demo"); -}finally { theSystem.out.println ("Proca ' s finally"); - }Wuyi } the - //Return from within a try block. Wu Static voidPROCB () { - Try { AboutSystem.out.println ("Inside PROCB"); $ return; -}finally { -System.out.println ("PROCB ' s finally"); - } A } + //Execute a try block normally. the Static voidProcc () { - Try { $System.out.println ("Inside PROCC"); the}finally { theSystem.out.println ("Procc ' s finally"); the } the } - in Public Static voidMain (String args[]) { the Try { the Proca (); About}Catch(Exception e) { theSYSTEM.OUT.PRINTLN ("Exception caught"); the } the PROCB (); + PROCC (); - } the}
In this example, Proca () prematurely interrupts the try by throwing an exception. The finally clause is executed at exit. The Try statement for PROCB () exits with a return statement. The finally clause executes before PROCB () returns. In Procc (), the Try statement executes normally with no errors. However, the finally block will still be executed.
Note: If the finally block is used in conjunction with a try, the finally block executes before the try ends.
Here is the output generated by the above program:
Inside Proca
Proca ' s finally
Exception caught
Inside PROCB
PROCB ' s finally
Inside PROCC
PROCC ' s finally
Series Articles:
Java know how much (top)
Java know how much (interface) interface
Java knows how much (40) the difference between an interface and an abstract class
Java know how much (41) generic explanation
Java know how much (42) the range of generic wildcard characters and type parameters
Java know how much (43) Exception Handling Basics
Java know how much (44) exception type
Java know how much (45) uncaught exceptions
How much Java knows (the) use of try and catch
Java know how much (47) use of multiple catch statements
Java knows how much (in) the nesting of Try statements
Java know how much (a) throw: Exception throws
Java know how many () Java throws clauses
Java knows how many (or) finally