• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

finally question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Brian and Val for your responses.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic