File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes nested try , catch , finally blocks ??? 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 , finally blocks ???" Watch "nested try , catch , finally blocks ???" New topic

nested try , catch , finally blocks ???

Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
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

Joined: Aug 05, 2005
Posts: 3742
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.


Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
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

Joined: Oct 13, 2005
Posts: 46349
Are you feeling brave? If so, try the Java Language Specification.
I agree. Here's the link:
subject: nested try , catch , finally blocks ???
It's not a secret anymore!