Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

try catch finally delicacy

Thread view: 
christian.bongiorno@gmail.com - 24 Feb 2006 17:22 GMT
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.