finally block in Exception

Discussion in 'Java' started by Matt, Jun 26, 2004.

  1. Matt

    Matt Guest

    I have question on finally block. If exception is catched, the statements inside
    finally block and beyond finally block still will execute. Then what's the point
    to put finally keyword?? Please advise. Thanks!!


    C:\>java ExceptionTest3
    abc.txt (The system cannot find the file specified)
    inside finally block...
    beyond finally block...

    --------------------------------------------------------
    import java.io.*;

    public class ExceptionTest3
    { public static void openFile() throws IOException
    { BufferedReader br = new BufferedReader(new FileReader("abc.txt"));
    String line = br.readLine();
    br.close();
    }

    public static void main(String args[])
    { try
    { openFile();
    }
    catch(Exception e)
    { System.out.println(e.getMessage());
    }
    finally
    {
    System.out.println("inside finally block...");
    }
    System.out.println("beyond finally block...");
    }
    }
     
    Matt, Jun 26, 2004
    #1
    1. Advertising

  2. Matt <> scribbled the following:
    > I have question on finally block. If exception is catched, the statements inside
    > finally block and beyond finally block still will execute. Then what's the point
    > to put finally keyword?? Please advise. Thanks!!


    I believe the purpose of the finally block is to ensure the code inside
    it *will* get executed no matter what happens: the try block completes
    normally, the try block throws an exception, or even the try block
    explicitly returns from the method.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
     
    Joona I Palaste, Jun 26, 2004
    #2
    1. Advertising

  3. Matt

    xarax Guest

    "Joona I Palaste" <> wrote in message
    news:cbkau7$7tv$...
    > Matt <> scribbled the following:
    > > I have question on finally block. If exception is catched, the statements

    inside
    > > finally block and beyond finally block still will execute. Then what's the

    point
    > > to put finally keyword?? Please advise. Thanks!!

    >
    > I believe the purpose of the finally block is to ensure the code inside
    > it *will* get executed no matter what happens: the try block completes
    > normally, the try block throws an exception, or even the try block
    > explicitly returns from the method.


    Furthermore, an exception is never catched. An exception is caught.

    Also, the catch(....) block may miss unchecked exceptions
    and the finally block is there to be sure that some code
    is executed before the stack is unwound to the calling
    method.
     
    xarax, Jun 26, 2004
    #3
  4. Matt

    Sudsy Guest

    Matt wrote:
    > I have question on finally block. If exception is catched, the statements inside
    > finally block and beyond finally block still will execute. Then what's the point
    > to put finally keyword?? Please advise. Thanks!!


    You've caught the exception but done nothing with it. Try modifying the
    code so that main throws IOException and remove the catch block. You'll
    find that the finally block executes but nothing beyond.
     
    Sudsy, Jun 26, 2004
    #4
  5. Matt

    Adam Maass Guest

    "Matt" <> wrote:
    > I have question on finally block. If exception is catched, the statements

    inside
    > finally block and beyond finally block still will execute. Then what's the

    point
    > to put finally keyword?? Please advise. Thanks!!
    >
    >


    Use finally to clean up resources, even if code generates an exception:

    public void foo() throws MyException
    {
    Integer i = null;

    try
    {
    i = new Integer(0);
    throw new MyException();
    }
    finally
    {
    i = null;
    }
    }


    Not so interesting with an Integer, but much more interesting with
    java.sql.Statement and Connection objects.

    -- Adam Maass
     
    Adam Maass, Jul 1, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    6
    Views:
    750
  2. Steve Claflin
    Replies:
    2
    Views:
    396
    Rhino
    Feb 13, 2006
  3. Efi Merdler

    Exception in finally block

    Efi Merdler, Dec 2, 2006, in forum: Java
    Replies:
    15
    Views:
    1,182
    Thomas Hawtin
    Dec 4, 2006
  4. morrell
    Replies:
    1
    Views:
    966
    roy axenov
    Oct 10, 2006
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    799
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page