Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return from catch block

 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
Can anyone please tell me if there is a return statement from the catch block, and there is a finally block, is the value from the catch block returned first.
I am little confused as how the code flows in the following program.

public class Test1 {

static int i=0;

public static void main(String[] args) {
System.out.println(meth1());
System.out.println(i);
}

static int meth1()
{

try
{
if(i==0)
{
throw new Exception();
}
}
catch(Exception e)
{
System.out.println(i);
return ++i;
}
finally
{
System.out.println(i);
return ++i;
}

}
}
The output is 0 1 2 2.

regards,
Raja
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The i value is first incremented in catch block and then in finally block. BTW why do you want to increment the value in catch and finally instead you could do it after finally block
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic