aspose file tools*
The moose likes Beginning Java and the fly likes Nested Try/Catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Try/Catch" Watch "Nested Try/Catch" New topic
Author

Nested Try/Catch

Sam Samson
Ranch Hand

Joined: Oct 08, 2011
Posts: 61

Hi

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

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?



Alex Armenteros
Ranch Hand

Joined: May 05, 2010
Posts: 73
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 05, 2010
Posts: 73
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested Try/Catch