• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions, throwing in a catch or finally bloack

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you write a bit of code and see what happens.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
I don't always make ads but when I do they're tiny
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic