my dog learned polymorphism*
The moose likes Java in General and the fly likes return statement in finally and checked Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "return statement in finally and checked Exception" Watch "return statement in finally and checked Exception" New topic
Author

return statement in finally and checked Exception

Jean John
Greenhorn

Joined: Dec 21, 2010
Posts: 24



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
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
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

Joined: Dec 21, 2010
Posts: 24

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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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

Joined: Dec 21, 2010
Posts: 24

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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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

Joined: Dec 21, 2010
Posts: 24

Thank You
 
GeeCON Prague 2014
 
subject: return statement in finally and checked Exception