File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PLease Explain Below Output

 
Akhilesh Yadav
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 344
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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..
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in case ,if there are statements after return will it prompt Unreachable code.
 
Vijendra Babar
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic