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

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

    public static void main(String[] args) throws Exception {
    try {
    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
    christian.bongiorno, Feb 24, 2006
    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.
    James McGill, Feb 24, 2006
    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(

    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.

    Bent D
    Bent C Dalager, Feb 24, 2006
  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.

    Raymond DeCampo, Feb 24, 2006
  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
  6. christian.bongiorno

    Oliver Wong Guest

    I believe the finally should execute AFTER the throw.

    - Oliver
    Oliver Wong, Feb 24, 2006
  7. Oliver is of course correct; after the throw, but before the thrown
    exception is caught..
    Mike Schilling, Feb 25, 2006
  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
    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.