Java Programming/Keywords/finally

From Wikibooks, open books for an open world
Jump to: navigation, search

finally is a keyword which is an optional part of the try block.

Example Code section 1: try block.
  1. try {
    
  2.   // ...
    
  3. } catch (MyException1 e) {
    
  4.   // Handle the Exception1 here
    
  5. } catch (MyException2 e) {
    
  6.   // Handle the Exception2 here
    
  7. } finally {
    
  8.   // This will always be executed no matter what happens
    
  9. }
    

The code inside the finally block will always be executed. This is also true for cases when there is an exception or even executed return statement in the try block.

Three things can happen in a try block. First, no exception is thrown:

Example Code section 2: No exception is thrown.
  1. System.out.println("Before the try block");
    
  2. try {
    
  3.   System.out.println("Inside the try block");
    
  4. } catch (MyException1 e) {
    
  5.   System.out.println("Handle the Exception1");
    
  6. } catch (MyException2 e) {
    
  7.   System.out.println("Handle the Exception2");
    
  8. } finally {
    
  9.   System.out.println("Execute the finally block");
    
  10. }
    
  11. System.out.println("Continue");
    
Standard input or output Console for Code section 2
Before the try block
Inside the try block
Execute the finally block
Continue

You can see that we have passed in the try block, then we have executed the finally block and we have continued the execution. Now, a caught exception is thrown:

Example Code section 3: A caught exception is thrown.
  1. System.out.println("Before the try block");
    
  2. try {
    
  3.   System.out.println("Enter inside the try block");
    
  4.   throw new MyException1();
    
  5.   System.out.println("Terminate the try block");
    
  6. } catch (MyException1 e) {
    
  7.   System.out.println("Handle the Exception1");
    
  8. } catch (MyException2 e) {
    
  9.   System.out.println("Handle the Exception2");
    
  10. } finally {
    
  11.   System.out.println("Execute the finally block");
    
  12. }
    
  13. System.out.println("Continue");
    
Standard input or output Console for Code section 3
Before the try block
Enter inside the try block
Handle the Exception1
Execute the finally block
Continue

We have passed in the try block until where the exception occurred, then we have executed the matching catch block, the finally block and we have continued the execution. Now, an uncaught exception is thrown:

Example Code section 4: An uncaught exception is thrown.
  1. System.out.println("Before the try block");
    
  2. try {
    
  3.   System.out.println("Enter inside the try block");
    
  4.   throw new Exception();
    
  5.   System.out.println("Terminate the try block");
    
  6. } catch (MyException1 e) {
    
  7.   System.out.println("Handle the Exception1");
    
  8. } catch (MyException2 e) {
    
  9.   System.out.println("Handle the Exception2");
    
  10. } finally {
    
  11.   System.out.println("Execute the finally block");
    
  12. }
    
  13. System.out.println("Continue");
    
Standard input or output Console for Code section 4
Before the try block
Enter inside the try block
Execute the finally block

We have passed in the try block until where the exception occurred and we have executed the finally block. NO CODE after the try-catch block has been executed. If there is an exception that happens before the try-catch block, the finally block is not executed.

If return statement is used inside finally, it overrides the return statement in the try-catch block. For instance, the construct

Example Code section 5: Return statement.
  1.  try {
    
  2.    return 11;
    
  3.  }  finally {
    
  4.    return 12;
    
  5.  }
    

will return 12, not 11. Professional code almost never contains statements that alter execution order (like return, break, continue) inside the finally block, as such code is more difficult to read and maintain.

See also: