aspose file tools*
The moose likes Beginning Java and the fly likes Please Help with the flow of this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please Help with the flow of this code" Watch "Please Help with the flow of this code" New topic
Author

Please Help with the flow of this code

Pratap Pandey
Greenhorn

Joined: Jun 27, 2007
Posts: 2
Please help me to interpret the o/p of this program.


public class exceptchk {
public static String op = "";
public static void foo(int i)
{
try
{
if(i==1)
{
throw new Exception();
}
op +="1";
System.out.println(op + "In TRY");
}
catch(Exception e)
{
op +="2";
System.out.println(op + "In Catch");
return;
}
finally
{
op +="3";
System.out.println(op + "In FINALLY");
}
op +="4";
System.out.println(op + "In Method");
}


public static void main(String[] args) {
// TODO Auto-generated method stub
foo(0);
foo(1);

}

}


The o/p is :
1In TRY
13In FINALLY
134In Method
1342In Catch
13423In FINALLY
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Pratap,

Is there anything in particular that you don't understand? Were you expecting the output to be different?

Regards,
JD
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
The general flow in try-catch block in java is as follows:
start of method->try->catch(exception raised)->finally ->return from method

start of method->try->method code after try-catch-finally(no exception thrown in try)->return from method


Thanks and Regards
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39861
    
  28
I find your flow difficult to understand. I presume you mean

try->exception->catch->finally->everything else

OR

try->no exception->finally->everything else.

The finally block is executed whether there is an exception thrown or not.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi Campbell

Thanks for correcting!! Yes finally is executed in any case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help with the flow of this code