aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Little confusion in Finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Little confusion in Finally block" Watch "Little confusion in Finally block" New topic
Author

Little confusion in Finally block

Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
Hello,

Here is a code sample from Enthuware.


I understand all of this. I am confused about the usage of two return statements. One in try block and other in
finally. Shouldn't return f cause a compilation error. Can somebody explain usage of this return in finally.

Thanks.
Arhaan
Achilleas Achix
Ranch Hand

Joined: Apr 18, 2011
Posts: 123

finally will always run and you will always get 10. Only Runtime.exit() can actually beat finally.


OCPJP 6.0
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi,

I think finally is always executed except we haven't called System.exit(); The point is whenever we use catch block, wrap the root exception in our custom exception and rethow it from catch block. In this case neither the try block return nor the catch block so finally block will return to the calling method. I think for this purpose compiler is made like this.

Please Confirm.

Thanks and Regards
Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
Hello,

I understand the working of the program and i know that finally always runs. I am just confused about the
usage of return statement inside finally. We do have a return statement in try block, even in exception block
and even in finally. I just wonder, what the purpose of using return statement in catch and finally. Specially,
why in finally. What will happen to the effect of return statement in try block.

Hope someone will be able to answer my query.

Arhaan
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 280
If you have a method that returns a value, you MUST specify a return statement in EVERY possible case. Which means that, if you catch an exception in that method, and your return statement is in the try block, then what is returned from the method? It is unknown, because if before your return statement, an exception is thrown, your method will catch this exception and run to its end but never find a return statement, which it must find to return a value and thus finish the method. So a return statement in a try block must never be the only return statement if the method returns a value.

it is uncommon to use a return statement in a finally block, because it effectively eliminates all choice of return value. Whatever you want to return, it will always be the finally block's return statement that runs before any other return statements, because finally must always run before your method returns.

I hope that answers your question.


Oracle Certified Professional Java Programmer
Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
Hello Dieter,

It definitely answers my query. Thanks a ton.

Thanks,
Arhaan
ashwathi rao
Greenhorn

Joined: Mar 26, 2009
Posts: 12
"it is uncommon to use a return statement in a finally block, because it effectively eliminates all choice of return value. Whatever you want to return, it will always be the finally block's return statement that runs before any other return statements, because finally must always run before your method returns. "



I did not understand this point still confused.
1)What is the order in which the return statements in try,catch,finally blocks get executed?

2)A finally executes no matter what the condition is ,so as I understand the method(handles the exception) that is calling the risky method(throws the exception) has a return statement at the end will be executed after the return statement in finally block .

3)The return statement in try,catch and finally blocks is what the risky method was supposed to return,because when the risky method throws an exception the control jumps to catch or finally block depending on what is written and the compiler still waits on a return value from the risky method.Is my understanding correct.

Appreciate your time and help .

ashwathi
Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
Hi,

The order in which return statements are executed are like this as per my understanding.

return statement in try block executes first if there is no exception and if there is a finally associated, it will run before returning from method.
in this case, catch block return statement will never run as there was no exception. Even if the exception occurs, first return statement of the
exception will execute then the return statement in finally.

Hope this helps.
Arhaan
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 280
ashwathi rao wrote:

1)What is the order in which the return statements in try,catch,finally blocks get executed?

2)A finally executes no matter what the condition is ,so as I understand the method(handles the exception) that is calling the risky method(throws the exception) has a return statement at the end will be executed after the return statement in finally block .

3)The return statement in try,catch and finally blocks is what the risky method was supposed to return,because when the risky method throws an exception the control jumps to catch or finally block depending on what is written and the compiler still waits on a return value from the risky method.Is my understanding correct.

Appreciate your time and help .

ashwathi

1. well, you have to choose between try and catch, it will only be one of those. The thing is, before the return statement of choice actually returns a value, the finally block must run to its end. In the finally block is also a return statement, and that one must run before the try/catch returns a value. Thus the finally block's return statement is handled first and the method returns before the try/catch gets a chance to return a value.

2. Everything in that statement will be handled first, but before it can return a value the finally block must run, because returning a value ends the method.

3. it doesn't matter to finally whether it's try or catch that tries to return a value, finally will block that return and return its own value.
Rohini Sahuji
Greenhorn

Joined: Jul 06, 2007
Posts: 19

Now think, if parameter passed, s, is null...

it will throw nullponterexception....
and it will go to finally...
now return from try is not getting executed......
here you can notice the significance of return in finally

Hope you get it...


SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Little confusion in Finally block
 
Similar Threads
JQ+ and confusion with try/catch/finally
Exception Handling
Find the unreachable statement in this code
Return
Unreachable Code in try/catch ? why????