Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finally question

 
satya rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try out this code ...

You never know what you are going to learn once you start coding
 
satya rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian and Val for your responses.
 
tiger hobbes
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic