aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finally question 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 question" Watch "finally question" New topic
Author

finally question

satya rao
Greenhorn

Joined: Feb 14, 2002
Posts: 9
Can anybody please look at the following code and explain me why it is printing "exception, finally, 5"?
public class Test {
public static void main(String[] str) {
System.out.println(new Test().method1());
}
int method1() {
int i = 0;
try {
method2();
System.out.println("int try");
return i;
}
catch (Exception e)
{
System.out.print("exception, ");
i = 5;
return i;
}
finally { System.out.print("finally, "); i = 2;}

}
void method2() { throw new NullPointerException(); }
}

I was under the impression that it would print "exception, finally, 2" as the finally block is executed at the end just before returning.
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
Could it be b'cos you have a return statement in the catch block and that returns 5?
You are setting i to 2 in the finally block but you are not returning 2.
Brian
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
in the catch block i has the value 5. Then when the return statement is reached, it stores that value (5) that is to be returned and then the control is transfered to the finally block. What the finally block does to the variable i does not have any effect on the return value because the latter has already been "stored" for good (but the value of i changes).


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

Joined: Nov 10, 2000
Posts: 165
Try out this code ...

You never know what you are going to learn once you start coding
satya rao
Greenhorn

Joined: Feb 14, 2002
Posts: 9
Thanks Brian and Val for your responses.
tiger hobbes
Greenhorn

Joined: Mar 04, 2002
Posts: 1
Thanks Brian and Val!
The sample code as well as Val's explaination give me a very clear picture of what's going on with the try.. catch...finally block.
But as to the sample code, i come with another question. If i remove the mark of finally block,i.e.,
finally { System.out.print("finally, "); i = 2;
j = 55;
// This will return a integer value of 2
return i;
}
and try to run it again, it seems my program fall into a endless loop printing "finally.....". Is ther anybody tried of this too? Could you please give me any explaiantion for this loop?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
hobbes,
Welcome to Javaranch
What "loop" do you mean ? If you remove the comment in the finally block and enable the "return" statement then 2 is returned and the output should be:
Value of j before calling method1: 99
exception, finally, 2
Value of j after calling method1: 55
I'm not sure I got what you meant... :roll:
Morever, we'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
prateek narang
Greenhorn

Joined: Apr 04, 2001
Posts: 19
I've tried that code and it's working fine.And the out put is
Value of j before calling method1: 99
exception, finally, 2
Value of j after calling method1: 55
--prateek
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: finally question