try-catch-throw-finally

Discussion in 'Java' started by Chris Riesbeck, Jan 19, 2007.

  1. Shouldn't the code below finish with an IllegalStateException, after the
    finally is executed? That's how I read Section 14.20.2 of the 3rd
    edition of the language spec. But with jdk1.5.0_06 on Windows XP, I'm
    getting the output "returned with no exception."

    14.20.2 Execution of try–catch–finally BLOCKS AND STATEMENTS

    "... If the catch block completes abruptly for reason R, then the
    finally block is executed. Then there is a choice:
    - If the finally block completes normally, then the try statement
    completes abruptly for reason R..."


    public class TryCatchFail {

    public static void main(String[] args) {
    System.out.println(doWithFinally());
    }

    private static String doWithFinally() {
    try {
    throw new IllegalArgumentException("io exception");
    }
    catch (IllegalArgumentException exc) {
    throw new IllegalStateException(exc);
    }
    finally {
    return "returned with no exception";
    }
    }

    }
    Chris Riesbeck, Jan 19, 2007
    #1
    1. Advertising

  2. Chris Riesbeck

    Oliver Wong Guest

    "Chris Riesbeck" <> wrote in message
    news:...
    > Shouldn't the code below finish with an IllegalStateException, after the
    > finally is executed? That's how I read Section 14.20.2 of the 3rd edition
    > of the language spec. But with jdk1.5.0_06 on Windows XP, I'm getting the
    > output "returned with no exception."
    >
    > 14.20.2 Execution of try–catch–finally BLOCKS AND STATEMENTS
    >
    > "... If the catch block completes abruptly for reason R, then the finally
    > block is executed. Then there is a choice:
    > - If the finally block completes normally, then the try statement
    > completes abruptly for reason R..."
    >
    >
    > public class TryCatchFail {
    >
    > public static void main(String[] args) {
    > System.out.println(doWithFinally());
    > }
    >
    > private static String doWithFinally() {
    > try {
    > throw new IllegalArgumentException("io exception");
    > }
    > catch (IllegalArgumentException exc) {
    > throw new IllegalStateException(exc);
    > }
    > finally {
    > return "returned with no exception";
    > }
    > }
    >
    > }


    14.21

    "A break, continue, return, or throw statement cannot complete normally."

    I.e.your finally block executes a return statement, and thus does not
    complete normally.

    - Oliver
    Oliver Wong, Jan 19, 2007
    #2
    1. Advertising

  3. Chris Riesbeck wrote:
    > Shouldn't the code below finish with an IllegalStateException, after the
    > finally is executed? That's how I read Section 14.20.2 of the 3rd
    > edition of the language spec. But with jdk1.5.0_06 on Windows XP, I'm
    > getting the output "returned with no exception."
    >
    > 14.20.2 Execution of try–catch–finally BLOCKS AND STATEMENTS
    >
    > "... If the catch block completes abruptly for reason R, then the
    > finally block is executed. Then there is a choice:
    > - If the finally block completes normally, then the try statement
    > completes abruptly for reason R..."


    The next case is the applicable one: "If the finally block completes
    abruptly for reason S, then the try statement completes abruptly for
    reason S (and reason R is discarded)."

    Your finally block completed abruptly, with reason a return of "returned
    with no exception", so the whole try-catch-finally completes abruptly
    for the same reason.

    If you want the return unless there is an exception, move it out of the
    finally block to the end of the method.

    Patricia
    Patricia Shanahan, Jan 20, 2007
    #3
  4. Patricia Shanahan wrote:
    > Chris Riesbeck wrote:
    >> Shouldn't the code below finish with an IllegalStateException, after
    >> the finally is executed? That's how I read Section 14.20.2 of the 3rd
    >> edition of the language spec. But with jdk1.5.0_06 on Windows XP, I'm
    >> getting the output "returned with no exception."
    >>
    >> 14.20.2 Execution of try–catch–finally BLOCKS AND STATEMENTS
    >>
    >> "... If the catch block completes abruptly for reason R, then the
    >> finally block is executed. Then there is a choice:
    >> - If the finally block completes normally, then the try statement
    >> completes abruptly for reason R..."

    >
    > The next case is the applicable one: "If the finally block completes
    > abruptly for reason S, then the try statement completes abruptly for
    > reason S (and reason R is discarded)."
    >
    > Your finally block completed abruptly, with reason a return of "returned
    > with no exception", so the whole try-catch-finally completes abruptly
    > for the same reason.
    >
    > If you want the return unless there is an exception, move it out of the
    > finally block to the end of the method.
    >
    > Patricia


    D'oh! I interpreted as "completes abruptly" as only referring to throw's
    not return's. Plus, something like

    try
    ...
    return n;
    catch
    throw ...
    finally
    ...

    looked to me like it would trigger a "path with no return value" compile
    error, but of course it doesn't.

    Thanks
    Chris Riesbeck, Jan 22, 2007
    #4
    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. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    357
    Kevin Spencer
    Aug 7, 2003
  2. VB Programmer

    The problem with Try Catch Finally...

    VB Programmer, Aug 12, 2003, in forum: ASP .Net
    Replies:
    23
    Views:
    762
    Chad Myers
    Aug 15, 2003
  3. Ralph Krausse

    ALL 'try/catch/finally' NOT created equal?

    Ralph Krausse, Aug 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    406
  4. hansiman

    try catch finally

    hansiman, Jul 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    478
    Sean M
    Jul 14, 2005
  5. David Lozzi

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

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

Share This Page