• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

return statement in finally and checked Exception

 
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi all,
Why is it that when you insert a return statement in finally block, java doesnt care if you have given throws in method declaration, or given a try catch where you are calling the function?
Awaiting Help,
Thanks In Advance
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the method always just returns and never throws an exception. There is no way a checked exception will be thrown so you don't need to declare it.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The contents of the finally block preempt anything that happens in the try and catch blocks. It is executed last, and so 'has last say' over how things return. This means that you can hide an exception in the try block by causing an exception in the finally block, or hide a return (either normal or exceptional) from the try block with a return from the finally block.

Needless to say (I think) - it is generally considered bad practice to put return statements in the finally block.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am actually somewhat impress that the compiler caught this -- it seems pretty dumb in many cases.

Henry
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:I am actually somewhat impress that the compiler caught this -- it seems pretty dumb in many cases.


Most compiler errors like are mandated by the JLS, not left to the discretion of the implementor. They didn't want programs to compile on some systems and not others. So it isn't really a matter of how smart or dumb the compiler is, but what depth of flow analysis was chosen by the JLS authors as standard. In this case, the rules are given in JLS 11.2.2. I think it makes sense that they had to put in special rules for handling try/catch, since otherwise you could never "handle" an exception as far as the compiler is concerned - and once they put in rules for try/catch, it would have been extremely foolish not to also consider how finally could affect the outcome.
 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still did not understand the discussion.. This thing works only with a return statement in finally block.. If I don't give a return statement in finally block, the compiler asks me to put a throws declaration or a try-catch.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:
Most compiler errors like are mandated by the JLS, not left to the discretion of the implementor. They didn't want programs to compile on some systems and not others. So it isn't really a matter of how smart or dumb the compiler is, but what depth of flow analysis was chosen by the JLS authors as standard. In this case, the rules are given in JLS 11.2.2. I think it makes sense that they had to put in special rules for handling try/catch, since otherwise you could never "handle" an exception as far as the compiler is concerned - and once they put in rules for try/catch, it would have been extremely foolish not to also consider how finally could affect the outcome.



Kinda already know this... but it still impresses me from time to time. I guess I just appreciates compiler technologies....

Henry
 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Mike Simmons wrote:
Most compiler errors like are mandated by the JLS, not left to the discretion of the implementor. They didn't want programs to compile on some systems and not others. So it isn't really a matter of how smart or dumb the compiler is, but what depth of flow analysis was chosen by the JLS authors as standard. In this case, the rules are given in JLS 11.2.2. I think it makes sense that they had to put in special rules for handling try/catch, since otherwise you could never "handle" an exception as far as the compiler is concerned - and once they put in rules for try/catch, it would have been extremely foolish not to also consider how finally could affect the outcome.



Kinda already know this... but it still impresses me from time to time. I guess I just appreciates compiler technologies....

Henry



So is the conclusion, that a return in finally is a rule mandated and if you see a return, you dont need any normal exception handling except for a try/finally??
I still dont understand why return statement gets this elite status ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jean John wrote:
So is the conclusion, that a return in finally is a rule mandated and if you see a return, you dont need any normal exception handling except for a try/finally??
I still dont understand why return statement gets this elite status ?



There is no "elite status". It is just that the analysis by the compiler deemed that there are no paths through the method that will actually throw the IOException. You can accomplish the same thing by replacing the return with a throw of a runtime exception. Or you can create a path by making the return conditional. See the link provided by Mike.

Henry
 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic