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
Joined: Aug 08, 2005
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
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.
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 ]
[ 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.
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.
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.
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.
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.