File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Nested try-finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Nested try-finally block" Watch "Nested try-finally block" New topic

Nested try-finally block

Marco Pacini

Joined: Mar 16, 2010
Posts: 5
I'm studying Exception Handling but i don't understand what happen in this code:
i wrote it to understand what happen when there are nested try block but i don't understand why InterruptedExcetion and ArrayStoreException aren't catched.
Nisha Ganeriwal

Joined: Feb 25, 2010
Posts: 8

This is how the try-finally block(Exception not caught) is handled: try block is executed. If theres is an exception, it is noted(I am not very sure on how) and the control is transferred to the finally block. After successful completion of the finally block the exception is passed on.

In your code, there is an exception(ArrayStoreException) in the inner-inner finally or the inner-try, this is noted and the inner-finally executed. Now inner-finally doesn't complete successfully so exception(ArrayStoreException) is forgotten(if it is correct to use the word). Now InterruptedException which occured in the try block(or inner-finally) is noted and control transferred to the outermost finally where Arithmetic Exception occurs causing finally to end abruptly and InterruptedException being forgotten. the Arithmetic Exception which occurred in the finally-block is now passed on for handling.

Section 7.13 on the page explains how this exactly works.

To summarize, if finally ends abruptly, any exception in the try block is forgotten.

I hope that made some sense.
I agree. Here's the link:
subject: Nested try-finally block
It's not a secret anymore!