• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

finally block does not completely normally..

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its because of the return statement in finally block

Kayal
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can someone please explain this?
 
Kayalvizhi Umashankar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
R Sawant
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ???
 
R Sawant
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt get any such warning.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic