File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Return from catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return from catch block" Watch "Return from catch block" New topic
Author

Return from catch block

rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
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

Joined: Feb 20, 2007
Posts: 76
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return from catch block
 
Similar Threads
finally question
try, catch,finally
Reg. unreachable code
Return Statement and Finally
Enthuware questions