try catch finally delicacy

Discussion in 'Java' started by christian.bongiorno, Feb 24, 2006.

  1. Everyday I learn something new about Java. Ok, maybe not everyday but
    frequently enough

    Example:
    public class Test {
    private static final String logger = null;

    public static void main(String[] args) throws Exception {
    try {
    logger.toString();
    }
    catch (Exception e) {
    throw new IllegalArgumentException();
    }
    finally {
    System.out.println("finally run");
    }
    }

    }

    I did not know that the finally clause did not apply to the catch
    portion of the syntax. My questions are: Why not? What would be the
    harm? And, are there anymore caveats?

    Christian Bongiorno
    http://christian.bongiorno.org/resume.pdf
     
    christian.bongiorno, Feb 24, 2006
    #1
    1. Advertisements

  2. christian.bongiorno

    James McGill Guest

    What do you mean? After the NullPointerException is caught, the new
    RuntimeException is thrown, and before the method returns, the finally
    clause executes.

    The rationale is clearly specified. I think you should read this.

    http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.20.2
     
    James McGill, Feb 24, 2006
    #2
    1. Advertisements

  3. What do you mean by "did not apply"? When running this program, you
    will first see the output from the finally clause and then you'll have
    the IllegalArgumentException reported, like this:

    $ java Test
    finally run
    Exception in thread "main" java.lang.IllegalArgumentException
    at Test.main(Test.java:9)
    $

    I would say that this means the finally clause _does_ "apply" to the
    catch block.

    It only gets troublesome when the finally block throws yet an
    exception, but that is something of a different scenario.

    Cheers
    Bent D
     
    Bent C Dalager, Feb 24, 2006
    #3
  4. I'm not sure what you mean by this. Code in the finally clause is
    executed after the try block and any catch blocks have finished,
    regardless of whether an exception is thrown or not.

    HTH,
    Ray
     
    Raymond DeCampo, Feb 24, 2006
    #4
  5. Ok, my bad. That was embarassing. I originally thought as per the posts
    here, but things began to make me suspect otherwise. I now notice in my
    own test that indeed the finally was executed before the throws.

    My thanks to those who made me check my output yet again.
     
    christian.bongiorno, Feb 24, 2006
    #5
  6. christian.bongiorno

    Oliver Wong Guest

    I believe the finally should execute AFTER the throw.

    - Oliver
     
    Oliver Wong, Feb 24, 2006
    #6
  7. Oliver is of course correct; after the throw, but before the thrown
    exception is caught..
     
    Mike Schilling, Feb 25, 2006
    #7
  8. christian.bongiorno

    Paul Hamaker Guest

    It's a matter of where we are in the call-stack. finally is meant to
    cleanup, close etc. before an uncaught exception is handled higher-up
    in the call-stack.
    Otherwise we wouldn't need finally at all.
     
    Paul Hamaker, Feb 25, 2006
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.