try-catch-throw-finally

C

Chris Riesbeck

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";
}
}

}
 
O

Oliver Wong

Chris Riesbeck said:
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
 
P

Patricia Shanahan

Chris said:
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
 
C

Chris Riesbeck

Patricia said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top