• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return statement in finally and checked Exception

 
Jean John
Greenhorn
Posts: 24
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank You
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic