File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finally block does not completely normally.. 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 does not completely normally.." Watch "finally block does not completely normally.." New topic
Author

finally block does not completely normally..

Ramdas Sawant
Ranch Hand

Joined: Aug 08, 2005
Posts: 51
Hi all,
I am getting the following warning after compilation of the following code
Warning: "finally block does not complete normally"

code::
public static String met1(){
String txt = "";
boolean b = true;
try{
if(b)
txt="true";
else
txt="false";

}
catch(Exception e){
txt = "Exception";
}
finally{
return txt;
}

}
-----------------------------------
Can any one explain this ?

Thanks
Ramdas.


If you think you can, you can. If you think you can't, you are right.
Kayalvizhi Umashankar
Greenhorn

Joined: Aug 04, 2005
Posts: 25
Its because of the return statement in finally block

Kayal
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 528

can someone please explain this?


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Kayalvizhi Umashankar
Greenhorn

Joined: Aug 04, 2005
Posts: 25
finally block is meant for cleaning up after an exception occurs. Hence the return statement in it contradicts the purpose. Thats why the warning message is generated.

This is what i understand

Kayal
Ramdas Sawant
Ranch Hand

Joined: Aug 08, 2005
Posts: 51
Originally posted by Kayalvizhi Umashankar:
finally block is meant for cleaning up after an exception occurs. Hence the return statement in it contradicts the purpose. Thats why the warning message is generated.

This is what i understand

Kayal

finally block is to perform the operation which has to be performed irrespective of whether the exception is thrown or not. I am doing the same. I don't get what u want to say in ur above reply.

Ramdas.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162


As you can see this code throws an RuntimeException and the output should be like this:

java.lang.RuntimeException: Test
at test.A.foo(A.java:12)
at test.A.main(A.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Exception in thread "main"

(In case you using IDEA like I do )

But if you put return statement inside finally block you will not get RuntimeException and stack trace. It will looks like RuntimeException was never thrown.
[ August 10, 2005: Message edited by: George Bolyuba ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi george ..
i have tried ur code....with no return statement it is throwing the run time exception ..but when we use the return statement then it is working fine and well ...

why is it so ???can u please tell me the flow of execution ???


Thanks & Regards<br /> <br />-Srikanth
Ramdas Sawant
Ranch Hand

Joined: Aug 08, 2005
Posts: 51
Originally posted by George Bolyuba:


As you can see this code throws an RuntimeException and the output should be like this:

(In case you using IDEA like I do )

But if you put return statement inside finally block you will not get RuntimeException and stack trace. It will looks like RuntimeException was never thrown.

[ August 10, 2005: Message edited by: George Bolyuba ]


Hi George,
I am not able to get the explaination given by u in this context. What u r talking about is fine. But how it is related with my problem of the warning. Can u just explain us. I just want to return a value in the finally block irrespective of whether the exception is thrown or not.

Ramdas.
AnuragSudha Jain
Greenhorn

Joined: Aug 10, 2005
Posts: 6
I didnt get any such warning.
Sabine K�ppers
Greenhorn

Joined: Aug 08, 2005
Posts: 3
i'm not sure if i understood yo correctly....sorry my english is not the best...

which enviroment do you use? Eclipse?
i.e. in eclipse Preferences- Java - Compiler Tab Style
there is 'finally' does not complete nomally --> which level do you use?
if the level is warning you get your warning message as you said.
if you trying ignore instead of warning you've got no message.

in addition i've found this in the web...
see
http://forum.java.sun.com/thread.jspa?threadID=516285&messageID=2460537

Re: About Finally block
Author: jverd Apr 23, 2004 10:22 AM (reply 5 of 8)
> he guys i m in problem so need u'r help
>
> my question is
> Whether we can use return statement in finally block
> or not !!!

You can, but don't. You should never return or throw an exception from a finally block. The finally block is there as a last stop on the way out of the try statement. It should just do cleanup. It should do any business processing of its own, so you don't want the finally block changing how the try statement is completing.

If the try or catch block is throwing an exception, and you return from finally, you're masking how your "real" code is completing. Or if finally throws an exception, it will mask the real exception or return that came from the completion of the business logic.
Ravi Nistala
Ranch Hand

Joined: Jul 11, 2002
Posts: 93
hello Ramdas :

I ran the program in eclipse and i did not get any error.I dont understand why r u getting compile error.

As somebody pointed out previously in this post that Finally should be used for cleaning purposes. But there is no such restriction saying that you should not use 'return' in finally.

let me know if anyboday has a different view on this.
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
I didn't get any warning either (I'm using jdk 1.4)

Srikanth, the reason why exception is not displayed when
return statement is : you are not catching the exception
so before ducking this exception to JVM, finally clause
is executed and whatever is within the finally clause
rules, i.e., 'return' statement makes the code to exit
cleanly (this is another example of 'lost exception')
because the caller is not even aware that an exception
occured. Hope this explains your question.
ritwik roy
Greenhorn

Joined: May 13, 2005
Posts: 20
hai Ramdas
i have tried your your first code (return txt in finally block), but i have not got any compile time or run time error/warning . It is working perfectly.
I am using jdk 1.5 in Windows 2000 professional.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finally block does not completely normally..