Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

why this finally block behaves this way

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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{}.
 
Saloon Keeper
Posts: 13505
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic