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 finally block with return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finally block with return statement" Watch "finally block with return statement" New topic

finally block with return statement

saiprasad raut
Ranch Hand

Joined: Aug 29, 2003
Posts: 40
Hi All,

please go through the below code

The ArithmaticException thrown in the computeAverage() method anpropogated to the printAverage() method is nullified by the return statement in the finally block.

Why is it so ? why does return in finally block nullifies the exception? is it only for unchecked exceptions?

Ana Abrantes
Ranch Hand

Joined: Sep 04, 2003
Posts: 43
Yes, that's because it's an unchecked exception.
But if you had a catch statement you could have caught that runtime exception.

Ana<p>SCJP 1.4
David Ng

Joined: May 11, 2004
Posts: 12
I don't believe the type of exception matters. If a return is executed within a finally block, it will nullify all exception (including pending one). After control is retuned to the calling method, execution will continue as normal.

The program below will run to normal completion and prints "Normal termination".

However, if line 20 is removed or commented out, it will terminate with the following:

My Exception
at Q0001Exception.method(
at Q0001Exception.main(
Exception in thread "main"

savas karabuz

Joined: Feb 24, 2004
Posts: 16

The following page clarifies the situation, i think :

JAC_061: Do Not Return From Inside A try Block (High)

Also, jdk1.4 javac compiler should issue a warning in such a situation.

[ May 27, 2004: Message edited by: savas karabuz ]

Joined: Aug 13, 2003
Posts: 4
m getting compile error when i try to return from finally. Execution is not getting continued at any point of time
says---finally clause cannot complete normally
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023

Welcome (back) to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!


If you've a compiler error you're trying to address, I'd suggest posting a question in the Beginners forum, including the code that you're trying to compile and the exact error message you're getting.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: finally block with return statement
It's not a secret anymore!