aspose file tools*
The moose likes Beginning Java and the fly likes return stmt. in try/catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return stmt. in try/catch block" Watch "return stmt. in try/catch block" New topic
Author

return stmt. in try/catch block

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Hi,

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

Thanks,
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I wonder if there might be a way to test that...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Finally block always executes, unless you pull the plug before it does.


SCJP 1.5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Hey, Thanks for all your replies.
Rob, thanks for elaborating the answer.
[ September 20, 2007: Message edited by: Sheethal Reddy ]
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Placing return in try{} gives error: missing return!
but in finally its ok.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

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!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Thanks Once again for all your replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return stmt. in try/catch block