File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why this finally block behaves this way

 
Anil Deshpande
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Anil Deshpande
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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{}.
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should generally avoid having final blocks end 'abruptly'. Avoid throwing exceptions in them, and *never* call return in them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic