File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes why this finally block behaves this way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "why this finally block behaves this way" Watch "why this finally block behaves this way" New topic
Author

why this finally block behaves this way

Anil Deshpande
Ranch Hand

Joined: Jan 13, 2008
Posts: 119
Have a look at this code


This works perfectly fine. But if i comment return statement its says unhandled exception compilation error.



I still have finally block after try statement. why is this compilation error just bacause I have removed return statement

Please clarify

Thanks and Regards
Anil Deshpande


Anil Deshpande
SCJP 1.5, SCWCD 1.5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

14.20.2 Execution of try-catch-finally
If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:

* If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
* If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).


[My Blog]
All roads lead to JavaRanch
Anil Deshpande
Ranch Hand

Joined: Jan 13, 2008
Posts: 119
I went through the specification. It's really confusing :-(.

Can you suggest some article which explains it in simple manner.

Thanks and Regards
Anil Deshpande
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It says:
-If there is an exception in the try{} block which is not caught in a catch{} block then then finally{} block gets executed and
-- If the finally block completes 'normally' then the exception thrown in the try{} block gets thrown from the end of the try{}finally{} statement (i.e. you 'See' the exception caused in the try{} block)
-- If the finally block completes 'abruptly' then whatever the results of the finally{} block are - that's what gets 'seen' as a result of the try{}finally{} block (for example if a new exception occurred in the finally{} you would see the new exception, not the one in the try{}).

In your first piece of code the finally block completes 'abruptly' with the return statement. So the result of the try{}finally{} is the return, not the exception.

In your second piece of code the finally block completes 'normally' because there is not exception, exit, or return statement. So the result of the try{}finally{} is the exception thrown from the try{}.


Steve
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

You should generally avoid having final blocks end 'abruptly'. Avoid throwing exceptions in them, and *never* call return in them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why this finally block behaves this way