Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

finally  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can we include return statements in finally block, meaning
try{}finally{return something} ?

Below is an example which will not compile where a return statements is placed in the finally block. If a return statement is allowed, please give me an example that compiles, just saying that return statements are allowed doesn't help me much Thanks for solving this puzzle for me.


(Code tags added)
[ June 11, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, your code will compile if you suppress "warnings". The issue is that when you "return" a value within certain blocks of code, it is considered "abrupt." "abrupt" changes are bad, so the compiler warns you about them. In your example, it doesn't really make much of a difference.

You can check out the relevant portion of the language spec here.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can a go to this link for further clarification

http://www.coderanch.com/t/245514/java-programmer-SCJP/certification/finally-block-return-statement
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i execute the above code i get a warning but other than that the code executes
C:\>javac -classpath . Example1.java
Example1.java:39: warning: finally clause cannot complete normally
}
^
1 warning

C:\>java -classpath . Example1
IllegalStatement
2

but there are two returns on in the catch and one in the finally
Is this legal?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is not legal to return twice from a function.

if we have one try block which returns something and there is one return statement in the finally block then it should not work.

how the same function return values twice??

Could someone explain???

Thanks
Kaps
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sounds plausible that in this special case the compiler would complain because it cannot execute the return in the finally block AND the return in the try block yet according to the language specs it would have to.

I've not tried it myself though, I'd set some variable in the try block (and to another value in the catch block) and return only in the finally block...
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code compiles fine. You can ignore the warning if u choose. It wo0uld however be a bad practice.

In try-catch-finally blocks if a exception (un-handled) say �FromTry� occurs in the try block (or even in catch) and it has no match in the catch blocks then whether this exception is passed on upwards depends on the finally block execution.

Finally block itself threw an exception�FromTry� is lost and it is the exception thrown from finally that goes up or gets propogated.

A control transfer like return or labeled break happens�FromTry� is lost. Also any value returned by the try or catch block is lost if some value is returned in finally. No exception is propogated up.

Finally did not throw an exception and there was no control transfer in finally�FromTry� is propogated up.

If any exception is propagated up then the method is aborted.
Therefore avoid returning values in finally. Use finally only for surefire cleanup.
 
Pal Sudarshan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for clearing up doubts
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!