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

Exception

 
arivu mathi
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test2{
public static void main(String args[]){
System.out.println(method());
}
public static int method(){
try{
throw new Exception();
return 1;
}
catch(Exception e){
return 2;
}
finally{
return 3;
}
}
}

i'm getting unreachable statement as compiler error....
can u explain ....
 
K Sujit
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be because of the return 1; statement, I hope you won't get the compiler error if you remove that.

And then if you run it may return you 2. I don't understand how return 3; is going to behave.

Hope somebody will execute it and tell.
 
Dinesh Bang
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here in the above code :
the statement "return 1;" is unreachable as it comes after the explicit throw exception statement. Thus the statements written after the throw in the try block are all unreachable codes, as they will never be executed.
In this code statements ,
the finally block doesn't work normally as even after the return from catch block, the finally block has to run and return value.
Thus we get o/p 3.
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More About the Finally Clause
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic