*
The moose likes Beginning Java and the fly likes Exceptions, throwing in a catch or finally bloack Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exceptions, throwing in a catch or finally bloack" Watch "Exceptions, throwing in a catch or finally bloack" New topic
Author

Exceptions, throwing in a catch or finally bloack

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hi all,
Bit stumpped here, what happens if you an exception is thrown in a catch or finally block, when a try block already has thrown an exception?

So for example your try has thrown an exception, which is passed to catch, catch attempts an operation that throws and exception.

Is it the first or second exception that gets reported to the user?

I would expect the second exception to be reported and the first to be lost.

Thanks
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3496
    
  13
Why don't you write a bit of code and see what happens.


Joanne
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Joanne Neal:
Why don't you write a bit of code and see what happens.


Reading the Java Language Spec, or a good book, would be better. While one cannot learn programming without doing some programming, it is all too easy to draw the wrong conclusions if one tries to learn purely by experiment.

In this particular question, I think it's reasonable to answer it.

Throwing in a catch block is perfectly reasonable. The original exception has been handled, by catching it. If something further goes wrong, throwing another exception is fine. This exception will not be caught in the same try..catch construct as the one you just caught; it will go to the nearest one outside.

Note that a catch block can catch an exception, do some stuff (which shouldn't throw), then re-throw the same exception. That is a common practice, actually. The re-thrown exception will not be caught in the same try..catch construct; it will go to the nearest one outside.

If a finally block is entered while an exception is being thrown, that original exception is lost, and replaced with the new exception. This is bad, so throwing in a finally block is most inadvisable. As well as not explicitly throwing an exception, you should also take care over exceptions that might occur due to method calls within "finally".


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3496
    
  13
Originally posted by Peter Chase:
Reading the Java Language Spec, or a good book, would be better.


I naturally assumed the OP would have done this before posting a question on here
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Thank you for your response

I was thinking more of bad programming, more in the case where you close a connection but the connection object is null. (yes you should check first).

I think my thoughts were correct, and we did write some code to see, but before we wrote teh code we did a quick search here, and a little skim of the sun tutorial on Exceptions and couldnt see what we were looking for.

I think my SCJP explains this, and I thought i had remembered, but didnt have it too hand to check.

We wrote a little programmer, and it worked as described. Interestingly when throwing an exception int the finally block Eclipse gave a warning that we shouldnt attempt such a thing

I have never read the Language spec, just the thought scares me

Once again thank you for taking the time to reply.

Gavin
[ January 18, 2008: Message edited by: Gavin Tranter ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
We wrote a little programmer, and it worked as described. Interestingly when throwing an exception int the finally block Eclipse gave a warning that we should attempt such a thing

Actually, this is a bug which I have seen a number of times. Consider something like this (don't worry if the compiler will complain):

try {
execute some code ..
}

catch (Exception e) {
throw new SomeException()
}

finally {
throw new AnotherException()
}

As finally must excecute, AnotherException is thrown before SomeException. But when AnotherException is thrown, the method ends! So, SomeException is never thrown, it is silently lost with no evidence in any log. This is why you must never execute any code in a finally block which causes the method to prematurely terminate.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions, throwing in a catch or finally bloack