• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nested try , catch , finally blocks ???

 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have

If it is caught in the inner try block then will the outer finally block be called?
And if you throw an exception from the first catch block where does it go. Is it like an exception occuring in the outer most try block so it goes to the outer most try block or is it go to the next catch block in the inner try that is of that type assuming you have one?

Some questions
If you have nest finally blocks then if the inner most finally is called does the outer ones get called as well or do you have to rethrow it?
If the outer most try block gets called then then does the inner finally blocks get called?
If you catch an exception in the inner try block and throw an exception does the new exception get caught in one of the parent try blocks?
What happens if you throw a new exception that is a superclass of any exceptions that the parent catch blocks can catch?


[HENRY: Add Code Tags, And formatted code]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to learn is to experiment. Try putting some code that throws exceptions inside your try block and see what happens. Try different exceptions/multiple catch blocks/no catch block (just a try and finally).
If you get any results you weren't expecting or don't understand, then come back and ask about them specifically.

Joanne
 
Sam Doder
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I figured out most of the questions but I still don't know about the finally block.
Does it get called automatically after you leave the try block. Or After you leave the entire function.
And will the finally block be always called no matter where it is or how many nested try blocks you have?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you feeling brave? If so, try the Java Language Specification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic