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 Try - catch - finally return values (again) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Try - catch - finally return values (again)" Watch "Try - catch - finally return values (again)" New topic
Author

Try - catch - finally return values (again)

Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Just when I thought I had this down, I get confused again. Can anyone explain the output of the following code:

The output is:
"The output of t.someMethod() = 1"
[ February 12, 2002: Message edited by: Bob Graffagnino ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
First off, the output is:
catch has assigned i the value 1.
finally has assigned i the value 2.
The output of t.someMethod() = 1
An exception is thrown within the try block, so the catch block will, well, catch it and assign 1 to i. Then, when the return statement is encountered, the value of i is stored somewhere as the return value. Just before returning, the finally block is executed, the assignment of 2 to i has no effect. The finally block ends and the method returns...
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
How about that. Pretty sneaky if you ask me!
As always Valentin - thank you.
aymen esawey
Ranch Hand

Joined: Jan 29, 2002
Posts: 61
hi
my question here is :its not guaranteed that the catch statement is always executed as mentioned in another thread,
and the compiler knows that in try catch finally block the finally block is always executed even if no exceptions result from try.
so why the compiler didn't say:
"missing return statement"?
i compiled it and i see it runs fine but i'm wondering if there is an explanation .i know there is agood one but i don't know it.


Aymen Esawey<br />SCJP <img src="smile.gif" border="0"> <br /><a href="http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=32&t=001968" target="_blank" rel="nofollow">how to nuke the SUN </a>
SaiPrasad Jukalkar
Ranch Hand

Joined: Feb 20, 2001
Posts: 79
i am also confused on seeing the above code.

if a function is returning a value, and if there is a try/catch block, sometimes we may have to return a value from each block.
pl correct me if iam wrong.


JSP
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
The compiler is smart enought o know all of the possible ways for the method to exit and can check them for proper return values. Inthe example given the code will always throw an Exception so the catch will always be exectued and the method will always return a value as required. To see the compiler check the code look at this:

When compiled the way it is above the compiler will produce an error saying the method does not return a value because with line 1 commented out and line 2 uncommented it will throw an uncaught NumberFormatException, so the exception is thrown then finally executes and at no point does it get to a return statement.
If you uncomment line 1 and comment line 2 it will still give you an error because you have a catch block that does not have a return statement.
So the compiler can figure out all of the exits from your method and test the for correctness - that's why you dont get an error in the original code.
This example uses RuntimeExceptions but the principle will be the same for checked Exceptions too.
hope that helps clear it up for you


Dave
SaiPrasad Jukalkar
Ranch Hand

Joined: Feb 20, 2001
Posts: 79
thanks Dave, i got it, your explaination is great
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Try - catch - finally return values (again)