Use of 'finally' in Java

By: Reema sen Viewed: 153730 times  Printer Friendly Format    


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, via 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.

Here is an example program that shows three methods that exit in various ways, none without executing their finally clauses:

// Demonstrate finally.
class FinallyDemo {
// Through an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
} finally {
System.out.println("procA's finally");
}
}
// Return from within a try block.
static void procB() {
try {
System.out.println("inside procB");
return;
} finally {
System.out.println("procB's finally");
}
}
// Execute a try block normally.
static void procC() {
try {
System.out.println("inside procC");
} finally {
System.out.println("procC's finally");
}
}
public static void main(String args[]) {
try {
procA();
} catch (Exception e) {
System.out.println("Exception caught");
}
procB();
procC();
}
}

In this example, procA( ) prematurely breaks out of the try by throwing an exception. The finally clause is executed on the way out. procB( )'s try statement is exited via a return statement. The finally clause is executed before procB( ) returns. In procC( ), the try statement executes normally, without error. However, the finally block is still executed.

Note: If a finally block is associated with a try, the finally block will be executed upon conclusion of the try.

Here is the output generated by the preceding program:

inside procA
procA's finally
Exception caught
inside procB
procB's finally
inside procC
procC's finally



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Asking questions are actually pleasant thing if yo
View Tutorial          By: CoD BO3 Hacks at 2015-10-31 11:19:44

2. Hey! Someone in my Facebook group shared this site
View Tutorial          By: hoodies for men at 2016-10-23 04:25:34

3. Winstonmog
View Tutorial          By: Winstonmog at 2017-02-20 15:24:34

4. JasonNix
View Tutorial          By: JasonNix at 2017-03-01 04:51:26

5. JasonNix
View Tutorial          By: JasonNix at 2017-03-05 03:00:58

6. JasonNix
View Tutorial          By: JasonNix at 2017-04-10 09:19:59

7. ThomasDix
View Tutorial          By: ThomasDix at 2017-04-19 01:47:02

8. Alvinrap
View Tutorial          By: Alvinrap at 2017-07-21 11:30:03

9. Alvinrap
View Tutorial          By: Alvinrap at 2017-08-06 11:02:14

10. Exception handling is very important concept in ja
View Tutorial          By: priya at 2017-09-11 06:06:50