Java Exceptions  «Prev  Next»

Lesson 10 The finally keyword in Java
ObjectiveCode that uses the finally keyword.

finally Keyword Block in Java

Determine the results of a block of code that uses the finally keyword.
Before we leave our topic of exceptions, we need to cover one tenacious keyword called finally. The finally keyword is a strange animal. If you're used to a certain flow of control, get ready for a jolt when you use finally.
A finally block of code can be defined after a try, as in:

try {  
  // do something  
}
finally {  
  // always do this, no matter what!  
}  

finally keyword in Java

When exceptions are thrown, execution in a method takes a rather abrupt, nonlinear path that alters the normal flow through the method. Depending upon how the method is coded, it is even possible for an exception to cause the method to return prematurely. This could be a problem in some methods. For example, if a method opens a file upon entry and closes it upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. The finally keyword is designed to address this contingency.
finally creates a block of code that will be executed after a try /catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. If an exception is thrown, the finally block will execute even if no catch statement matches the exception. Any time a method is about to return to the caller from inside a try/catch block, by means of an uncaught exception or an explicit return statement, the finally clause is also executed just before the method returns. This can be useful for closing file handles and freeing up any other resources that might have been allocated at the beginning of a method with the intent of disposing of them before returning. The finally clause is optional. However, each try statement requires at least one catch or a finally clause.
What do we mean by no matter what? Even if you break, return, or throw an exception in the try block, the finally code will always, and we mean always, be executed. Here's some code that illustrates how you might define a finally block:

class Tenacious { 
	
 public static void main(String[] args) { 
  try { 
   myMethod1(); 
   myMethod2(); 
   myMethod3(); 
  }
  catch (Exception e) { 
   System.out.println("Catching exception"); 
  } 
 } 
 // ----- myMethod1()
 static void myMethod1() { 
  try {
   return; 
  }
  finally { 
   System.out.println("Attempted Return"); 
  } 
 } 
 // ----- myMethod2()
 static void myMethod2() { 
  top: 
   try { 
    break top; 
   }
   finally { 
    System.out.println("Attempted break"); 
   } // End Finally
 } 
 // ----- myMethod3()
 static void myMethod3() throws Exception { 
  try { 
    throw(new Exception()); 
  }
  finally { 
   System.out.println("Attempted to throw an exception"); 
  } 
 } 
} 

Here's the output:
we tried to return  

we tried to break  

we tried to throw an exception  

Catching exception 

Finally Keyword Quiz

This quiz pertains to the finally keyword previously discussed.
Finally Keyword - Quiz