File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. return from try-catch-finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. return from try-catch-finally" Watch "Reg. return from try-catch-finally" New topic

Reg. return from try-catch-finally

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
I have some confusion regarding the use of return statements in a try-catch-finally constructs.
Suppose i have some method which has to return a int value,

I have observed the following three condtion for valid use of return :
1. try - has return statement
catch - has return statement
finally- does not have return statement
End of method -NO return statement required
2. try - has return statment
catch - has return statement
finally- has return statement
End of method - NO return statement required
3. try- may have return statmenet
catch - may have return statement
finally - does NOT have the return statement
End of method - Should have return statement
What is the best way of remembering the sequence or return
usage in such type of try-catch-finally blocks ?

Any immediate reply would be nice.

[This message has been edited by Thomas Paul (edited October 09, 2001).]
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143
I think you need to remember the following.
The finally block will ALWAYS execute.
The Try block may throw an exception before getting to the return statement.
The Catch block may not execute.
So you could do either of the following...
1. Have returns in both the try and catch.
2. Just have the return in the finally block.

Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

Hi Angela,
If present, a return statement in finally *always* prevails. (you can add this to ur exception 11th hour notes), irrespective of the fact that try and/or catch includes it, with 2 exceptions -thread dead or call System.exit(0) inside try/catch block.
The return statement must be the last statement in all these blocks, if you have any statement after that the compiler will give error.
As from test point of view, remember that besides these two exceptions, the finally block is always going to be executed, even if there is a return statement in try/catch, before returning finally will get executed, hence if there is a return statement in finally, return value in finally block will *always* be the return value of the function.
In real life, typically function value will be different if function is successful (no exception) and when function is unsuccessful (exception occurs). It is a good programming practice to assign distinct values to the function return variable (say result) in a try and catch block and use only single return statement (returning this result) in the finally block after doing the required clean up.
Also, have a look at this.
- Manish
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The one thing to keep in mind is that a return in a finally will consume any generated exception. For example:

will return 10 even though we never caught any exception.
[This message has been edited by Thomas Paul (edited October 09, 2001).]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
In fact, any instruction within a finally that will leave out the finally clause (break, continue, return and throw) will eat the exception in try.

SCJP2. Please Indent your code using UBB Code
Raj Shah

Joined: Sep 24, 2001
Posts: 14
Hi Angela,
This same problem came to my mind as well. But after you have narrowed down the valid possiblities, what I figure out is that:
For a method returning some type:
execution path can be:-
try-->last block after catch -->return to calling program
try-->catch-->last block after catch-->return to calling program
try-->finally-->last block after finally-->return to calling program
try-->catch-->finally-->last block after finally-->return to calling program

01) The try block and last section of the method block forms a normal path. But as a try block is always associated with either a catch block or a finally block or both, there is no meaning of a return in try block if you specify a return in these later blocks.
02) The catch and the finally block form the abnormal condition and create an alternate path to the normal path. They are invoked internally by the jvm and they both can have a return specified (irrespective of a return specified in try block), but the last value returned will that be of finally block.
This catch/finally block being called after the try block takes care about the redundant return in try block.
03) any return present in finally OR in "catch W.O. finally" will transfer control to the program where you are calling this method and hence any thing within the method after this catch/finally code block , in such condition, will be unreachable.
04) also in try or catch or finally block there can be exception thrown before return. So In every later block that your program may call upon internally after throwing the exception, you may specify a return.
Raj Shah
Kami Brooks

Joined: Mar 13, 2001
Posts: 2
Ref "consume any generated exception":
If I add the catch statement below,
Will the the System.out.print will execute?
Will 10 or 20 be returned?

Originally posted by Thomas Paul:
[B]The one thing to keep in mind is that a return in a finally will consume any generated exception. For example:

will return 10 even though we never caught any exception.

[This message has been edited by Thomas Paul (edited October 09, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The catch is always run before the finally so k will be equal to 20.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hey the previous code will print (correctly) :
" Exception caught
The exception is thrown and caught.
In the first code (where no catch) the explanation is :
The exception is launched but is superseeded by the fact that the finally clause doesn't complete.
The compiler adds automatically in the code a catch clause, after all of them, for the finally clause. The object that this cacth clause traps is Throwable (so it catches all the exception). The code for this catch clause saves the exception just caught, jumps to the finally clause and when this one returns, rethrows the saved exception. Thus if the finally clause doesn't completes because of a return, break, continue or throw; the rethrowing of the exception does not occur. In this way the exception is lost.
The compiler places instruction at the end of the finally clause that returns to the point where the jump to the finally clause ocurred. Only if there is no continue, break, return , or no exception is thrown from within the finally clause this returning jump is executed. In this way is possible to lose an important exception thrown in a try without a catch.
Arjun Anand

Joined: May 24, 2001
Posts: 25
Actually you dont need a finally clause if you are catching the appropriate exceptions and want to return a value irrespective of whether that exception is thrown or not. The following code will also do fine.

public int intFunction() {
int k = 10;
try {
} catch ( Exception e) {}
return k;

The above code also returns the value 10.

I agree. Here's the link:
subject: Reg. return from try-catch-finally
It's not a secret anymore!