Results 1 to 5 of 5

Thread: What is the 'finally' Block in Java?

  1. #1
    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

  2. #2
    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) {
         
    }

  3. #3
    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.

  4. #4
    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

  5. #5
    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())
      ;
    }

Similar Threads

  1. Need help to block Java Animations in Firefox
    By triop in forum Software Development
    Replies: 4
    Last Post: 10-11-2010, 03:54 AM
  2. Equivalent to Java finally keyword in PHP
    By Jaganmohini in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:20 AM
  3. Difference between final, finally and finalize in Java?
    By Maddox G in forum Software Development
    Replies: 6
    Last Post: 31-01-2010, 11:09 AM
  4. How does C sharp support try-catch-finally block?
    By Trance Maniac in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:02 AM
  5. Java - brain block
    By Soft Pack in forum Software Development
    Replies: 2
    Last Post: 05-11-2008, 07:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,039,417.14269 seconds with 17 queries