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
James McGill - 24 Feb 2006 17:36 GMT
> Everyday I learn something new about Java. Ok, maybe not everyday but
> frequently enough
[quoted text clipped - 19 lines]
> I did not know that the finally clause did not apply to the catch
> portion of the syntax.
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
christian.bongiorno@gmail.com - 24 Feb 2006 18:55 GMT
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.
Oliver Wong - 24 Feb 2006 19:10 GMT
> I now notice in my
> own test that indeed the finally was executed before the throws.
I believe the finally should execute AFTER the throw.
- Oliver
Mike Schilling - 25 Feb 2006 02:19 GMT
>> I now notice in my
>> own test that indeed the finally was executed before the throws.
>
> I believe the finally should execute AFTER the throw.
Oliver is of course correct; after the throw, but before the thrown
exception is caught..
Paul Hamaker - 25 Feb 2006 10:37 GMT
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, SEMM, teaching ICT since 1987
http://javalessons.com
Bent C Dalager - 24 Feb 2006 17:38 GMT
>Everyday I learn something new about Java. Ok, maybe not everyday but
>frequently enough
[quoted text clipped - 5 lines]
>portion of the syntax. My questions are: Why not? What would be the
>harm? And, are there anymore caveats?
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

Signature
Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
powered by emacs
Raymond DeCampo - 24 Feb 2006 17:40 GMT
> Everyday I learn something new about Java. Ok, maybe not everyday but
> frequently enough
[quoted text clipped - 20 lines]
> portion of the syntax. My questions are: Why not? What would be the
> harm? And, are there anymore caveats?
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

Signature
This signature intentionally left blank.