• 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

Nested Try/Catch

 
Ranch Hand
Posts: 63
IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I don't have to catch the inner Exception, if an Exception is already caught by an outer catch block? Is that true?

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Samson wrote:
I don't have to catch the inner Exception, if an Exception is already caught by an outer catch block? Is that true?



Yes. And if you're even asking that question, you're overcomplicating things.



That means that if the "some code" part throws SomeException, it will be caught. With me so far?

Okay, so why would it matter if "some code" includes try/catch, try/catch/finally, or try/finally?



 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I see in your code.

you never get to enter the second try/finally block as an exception is thrown inside the f¡rst "doSomething", so you'd never see the "nested finally"

If line 5 were empty... the output would be

"do something"
"nested finally" //here an exception is thrown and the program is forced to exit the nested try block, as that block does nott have a catch clause, so that code portion is executed in that moment.
"catch"
"finally"
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Armenteros wrote:From what I see in your code.

you never get to enter the second try/finally block as an exception is thrown inside the f¡rst "doSomething", so you'd never see the "nested finally"



No, the nested finally will be executed. That's the whole point of finally. It's executed whether an exception is thrown or not, and if one is, it's executed whether one is caught or not. A return, exception, break, or continue out of a try or catch always transfers control to finally if it's present. (And a return, exception, break, or continue out of finally is almost always a bad idea.)
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's correct mister Verdegan, a finally block will execute when the execution exits its try block, but in mr. Samson's example the execution does not enter the nested try/finally block because an exception is thrown BEFORE entering that block.

I've tried the code personally and the output does not contain "nested finally".
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Armenteros wrote:That's correct mister Verdegan, a finally block will execute when the execution exits its try block, but in mr. Samson's example the execution does not enter the nested try/finally block because an exception is thrown BEFORE entering that block.

I've tried the code personally and the output does not contain "nested finally".



Oh, you mean if the first doSomething() call throws an exception? Then, yes, of course. I thought we were specifically talking about an exception in the nested try. I didn't even notice that doSomething() was actually implemented and threw an exception, or that it was called both before and inside the netsted try. Guess I should read more closely.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic