Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-02-2010
Member
 
Join Date: Jul 2006
Posts: 191
What is the 'finally' Block in Java?

Hi Friends,
I have just started with the topic Exceptions in Java. In that I have done try and catch method which truly speaking, I didn't understand much. But the finally block was very confusing for me. I didn't understand anything about that. So please help me by explaining what is the 'finally' Block in Java? Hope that you guys got my point.!!
__________________
ASUS P5VD1-X
Intel Pentium Dual Core 3.00GHz
Maxtor 160GB
Corsair 1.5GB PC3200 RAM
Nvidia Geforce 6800 GT 256mb
Phillips DVD RW
Magna 500W PSU
XION II Steel Black Gaming Case
Reply With Quote
  #2  
Old 18-02-2010
Member
 
Join Date: Mar 2008
Posts: 672
Re: What is the 'finally' Block in Java?

The finally block always executes when the try block exits. So you should be clear about the Try and Catch exceptions before moving to the finally block. The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block. The try block has syntax as follows :
Code:
try {
    code
}
catch and finally blocks . . .
After the try block you will have to provide the Catch block. You should not write any code between the the end of the try block and the beginning of the first catch block. Look at the following :
Code:
try {
     
} catch (ExceptionType name) {
     
} catch (ExceptionType name) {
     
}
Reply With Quote
  #3  
Old 18-02-2010
Member
 
Join Date: Mar 2008
Posts: 349
Re: What is the 'finally' Block in Java?

As said earlier, the finally block always executes when the try block exits. In short you can say that the Finally block in java is used along with the try-catch statements. The following is the normal structure :
Code:
try {
.....
} catch(Exception e){
....
} finally {
.....
}
In the finally block we usually have code that is used to perform clean up activities corresponding to the code in the try block. The exception that occurs in the try block control comes to the catch block and the rest of the code in the try block would not execute.
Reply With Quote
  #4  
Old 18-02-2010
Member
 
Join Date: Aug 2006
Posts: 235
Re: What is the 'finally' Block in Java?

The best use of finally block is when we are connecting to the database. In the try block we would have the statements that instantiate the connection, prepared statement, result set etc. Using of an exceptions is very important because if the query fails then all these objects would be left open and the code would continue. Also the finally block is very useful because we nullify these statements so that even in case of an error the unused objects are cleared. Hope that you got the point why we use the finally block..
__________________
3.2 (northwood)
2gig ram
ATI AIW X800xt 256mb
Gigabyte GA-8knxp 875p Chipset
Optiwrite 8X DVD Burner
Win XP PRO Sp2 (Works Perfectly)
2 SATA Raptor 74gig Raid 0
2 7200 IDE 320gig HD
Reply With Quote
  #5  
Old 18-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: What is the 'finally' Block in Java?

The following sample of the coding may help you :
Code:
import java.io.*;
import java.util.logging.*;

public final class FinallyDemo {
  
  public static void main(String... aArgs) {
    nestedFinally("C:\\Temp\\example.txt");
  }
  
  private static void FinallyDemo(String aFileName) {
    try {
      BufferedReader reader = new BufferedReader(new FileReader(aFileName));
      try {
        String line = null;
        while ( (line = reader.readLine()) != null ) {
        }
      }
      finally {
        reader.close();
      }
    }
    catch(IOException ex){
      fLogger.severe("Problem occured : " + ex.getMessage());
    }
  }
  
  private static final Logger fLogger =
    Logger.getLogger(FinallyDemo.class.getPackage().getName())
  ;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is the 'finally' Block in Java?"
Thread Thread Starter Forum Replies Last Post
Need help to block Java Animations in Firefox triop Software Development 4 10-11-2010 03:54 AM
Equivalent to Java finally keyword in PHP Jaganmohini Software Development 6 12-08-2010 10:20 AM
Difference between final, finally and finalize in Java? Maddox G Software Development 6 31-01-2010 11:09 AM
How does C sharp support try-catch-finally block? Trance Maniac Software Development 5 21-01-2010 09:02 AM
Java - brain block Soft Pack Software Development 2 05-11-2008 07:47 PM


All times are GMT +5.5. The time now is 11:16 AM.