Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
public class test{
static string s="";
public static void metod(int i){
try{
if(i==2){throw new Exception();
}
s+=1;
}
catch (Exception e)
{
s+=2;
return;}
finally
{
s+=3;
}
s+=4;
}
public static void main (String args[])
{
method(1);
method(2);
Sytem.out.println(s);
}
prints 13423
but i feel it should be 134234 since the exception is caught the line after finally should be executed.
Please could someone help me on this
Regards
Neha
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is no return in catch(){.... return;} you are right.
But there is a return statment. Before the return; in catch(){} the final {} gets to run and then back to the return statement.
I think you know this already, just make sure you read all the code.

[This message has been edited by FEI NG (edited December 02, 2001).]
 
Sonali Joshi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx FEING
I was not aware of this.
Now that my exam date is coming near i am getting all the more confused.
Thnk you once again
Regards
Neha
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come the same guy has two different names ??
Neha Sawant and Sonali Joshi, which one really exists?

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic