| Author |
PLease Explain Below Output
|
Akhilesh Yadav
Ranch Hand
Joined: Apr 04, 2006
Posts: 46
|
|
public class Car { String a="a"; String b="b"; String c="c"; public int foo(){ try{ System.out.println(" in Try "+a); return 1; } catch(Exception e){ return 2; } finally{ System.out.println(" in Finally "+c); return 3; } } public static void main(String [] args){ Car c= new Car(); if((c.foo())==3) { System.out.println(" in main "+c.a); } }} I was Expecting output only :-- in main a But when i run the programm i got this output :- in Try a in Finally c in main a please exaplin why ?
|
 |
Chandra shekar M
Ranch Hand
Joined: Dec 20, 2006
Posts: 134
|
|
Hi Yes the output should be in Try a in Finally c in main a The reason is when you called foo() it will print "in Try a" since there is a finally blk it is always guranteed to execute NOTE even when you have return statement return 1 in try blk it will execute finally blk this gives "in Finally c" and at last in finally blk you have return 3; so in main you get "in main a" . NOTE even though you have return compiler will not execute return statement insted it will come to finally blk but in finally blk you have return statement so control will not go back to try blk to return one instead it will return 3 from finally blk .Now if you had return only in try blk then control will come back to try blk in the sence to return 1; Thanks
|
 |
Micheal John
Ranch Hand
Joined: Nov 01, 2006
Posts: 344
|
|
public class Car { String a="a"; String b="b"; String c="c"; public int foo(){ try{ System.out.println(" in Try "+a); return 1; } catch(Exception e){ return 2; } finally{ System.out.println(" in Finally "+c); return 3; } } public static void main(String [] args){ Car c= new Car(); if((c.foo())==3) { System.out.println(" in main "+c.a); } }} I was Expecting output only :-- in main a But when i run the programm i got this output :- in Try a in Finally c in main a please exaplin why ?
In main method you are calling the foo(). Inside foo() you are having try-catch-finally block. Finally block always run even if you have exceptions or not. So first the try block print statement is printing and in try, there is no exception occured, so finally block will run even if you have a return statement in try block and in the finally block it's returning 3, which is equal to your comparsion in main..so main also prints..
|
Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
|
 |
Pratap koritala
Ranch Hand
Joined: Sep 27, 2006
Posts: 251
|
|
|
in case ,if there are statements after return will it prompt Unreachable code.
|
 |
Vijendra Babar
Greenhorn
Joined: Jul 28, 2006
Posts: 18
|
|
Note: The only way you can avoid execution of finally block is by writing System.exit(0) in try block. Following is modified Program: public class Car { String a="a"; String b="b"; String c="c"; public int foo(){ try{ System.out.println(" in Try "+a); System.exit(0);// Remember there is no meaning in having return 1, // It will not give error as Unreachable code return 1; } catch(Exception e){ return 2; } finally{ System.out.println(" in Finally "+c); return 3; } } public static void main(String [] args){ Car c= new Car(); if((c.foo())==3) { System.out.println(" in main "+c.a); } }} Output: in Try a
|
 |
 |
|
|
subject: PLease Explain Below Output
|
|
|