Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return stmt. in try/catch block

 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can we have a return statement in try block. If yes, does it execute finally block?

Thanks,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if there might be a way to test that...
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally block always executes, unless you pull the plug before it does.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by greg buela:
Finally block always executes, unless you pull the plug before it does.

By which Greg means calling System.exit(...) or otherwise closing the JVM immediately.
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Thanks for all your replies.
Rob, thanks for elaborating the answer.
[ September 20, 2007: Message edited by: Sheethal Reddy ]
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Placing return in try{} gives error: missing return!
but in finally its ok.
 
Brian Cole
Author
Ranch Hand
Posts: 907
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ahmed Yehia:
Placing return in try{} gives error: missing return!
but in finally its ok.


You need to return something (or throw something) after you catch
the exception. You can do this in the catch clause, in the finally
clause, or later on past the entire try/catch/finally block. [edit: As
others have pointed out, it's generally not a good idea to return
from the finally clause.]
[ September 21, 2007: Message edited by: Brian Cole ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Brian Cole]: You need to return something (or throw something) after you catch the exception. You can do this in the catch clause, in the finally
clause, or later on past the entire try/catch/finally block.


Brian probably knows this, but for Sheethal - note that if you put a return inside a finally block, it's legal as far as the compiler and JVM are concerned - however it may well cause your fellow developers to hunt you down and kill you. Or at least maim you. Or force you to listen to Barry Manilow recordings. This is because when you put a return in a finally block, you cause the JVM to forget about whatever it was doing before the finally. So if you were already supposed to return something else, that other thing is forgotten and you instead return the new thing. Or if you were throwing an exception, that exception is now forgotten, and you return as if nothing bad happened. This typically makes it very difficult to debug your program, as you lose valuable stack trace info from the exception. Which may make your fellow developers very unhappy.

You really never need to put a return in a finally block. On the few occasions where you might want to, you can find a way to achieve the same effect more clearly by putting returns in a catch block, or after the whole try/catch/return.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15357
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ahmed Yehia:
Placing return in try{} gives error: missing return!
but in finally its ok.

Putting a return statement in a finally block is a bad idea!
If you do that, you are swallowing exceptions quietly.

See this post for a longer discussion.

Never put a return in a finally block!
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Once again for all your replies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic