try catch finally delicacy

  • Thread starter christian.bongiorno
  • Start date
C

christian.bongiorno

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
 
J

James McGill

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.

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
 
B

Bent C Dalager

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

Example:
( . . . )

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?

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
 
R

Raymond DeCampo

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?

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
 
C

christian.bongiorno

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.
 
M

Mike Schilling

Oliver Wong said:
I believe the finally should execute AFTER the throw.

Oliver is of course correct; after the throw, but before the thrown
exception is caught..
 
P

Paul Hamaker

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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top