java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3945
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Use of Finally Clause E-mail
User Rating: / 133
PoorBest 

When exception is 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, the method may return prematurely. For example, if a method opens a database connection on 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 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, finally block will execute even if no catch statements matches the exception. Finally is guaranteed to execute, even if no exceptions are thrown. Finally block is an ideal position for closing the resources such as file handle or a database connection etc.

public class FinallyDemo {

  static void procA(){

    try{
      System.out.println("inside procA");
      throw new RuntimeException("demo");
      
    }finally{
      System.out.println("procA's finally");
    }
  }

  //Return from with in 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();

  }

}

Output Screen:

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

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.