Java Programming/Keywords/finally

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

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

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

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.
System.out.println("Before the try block");
try {
  System.out.println("Inside the try block");
} catch (MyException1 e) {
  System.out.println("Handle the Exception1");
} catch (MyException2 e) {
  System.out.println("Handle the Exception2");
} finally {
  System.out.println("Execute the finally block");
}
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.
System.out.println("Before the try block");
try {
  System.out.println("Enter inside the try block");
  throw new MyException1();
  System.out.println("Terminate the try block");
} catch (MyException1 e) {
  System.out.println("Handle the Exception1");
} catch (MyException2 e) {
  System.out.println("Handle the Exception2");
} finally {
  System.out.println("Execute the finally block");
}
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.
System.out.println("Before the try block");
try {
  System.out.println("Enter inside the try block");
  throw new Exception();
  System.out.println("Terminate the try block");
} catch (MyException1 e) {
  System.out.println("Handle the Exception1");
} catch (MyException2 e) {
  System.out.println("Handle the Exception2");
} finally {
  System.out.println("Execute the finally block");
}
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.
 try {
   return 11;
 }  finally {
   return 12;
 }

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: