This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Return from finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Return from finally" Watch "Return from finally" New topic

Return from finally

Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40

I was trying some codes with exceptions and finally
The above code compiles but issues a warning : finally clause cannot complete normally. Also note that the function does not declare that it throws exception (from catch clause).
Can anybody please explain what's hapenning in the above code.
[ November 19, 2003: Message edited by: Vicky Nagarkar ]
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
If you throw a exception from a method , the method signature must be defined with throws , something like this
public int m() throws Exception {}

Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40
Yes I am aware of that.. but the above code compiles without
"public int m() throws Exception {}" with a warning.
if i remove the return from the finally and place it at the end of the function, then it gives an error that it should be declared in the throws class.
I am not getting what's hapenning
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
What is happening is as follows:
Your method m is throwing an exception in the try block, which is caught the catch block.
This catch block again rethrows the exception.
The rule for using a method which throws a checked exception is that either it should declare in its signature that it throws an exception or it should handle the exception within its body, and effectively not throw the exception. Now you are catching the exception and rethrowing it, so the control goes the finally block before coming out of the method.
In the finally block, by putting a return statement, you have ignored the exception that you threw in the catch block, and are providing a normal return value to be printed. Hence this value, 3 , will be printed.
Now for the second part of the question.
Had the return been put outside the finally block, as is the normal/best practice, it would have given an error saying statement not reachable because the control moves out of the method after the finally block.So the last return statement would never be reached because of the abnormal termination due to the exception you are throwing in the catch block.
Finally, I am not getting any warnings with your programs. Only errors for the conditions you said. Maybe because of different jdk versions.
Neways, I hope the flow is clear now.
Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40
Thanx Anupreet..
Got it now
I agree. Here's the link:
subject: Return from finally