aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in try catch finally Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "doubt in try catch finally" Watch "doubt in try catch finally" New topic
Author

doubt in try catch finally

Kanchan Narang
Greenhorn

Joined: Jul 07, 2007
Posts: 8
Hi All ,
code --
try{
int a=2/0;
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("Exception");
}

After running this I get O/p:
Exception
Excetion Trace

Is this the way it shud work ,,
Acc to me first catch will be done and then finally block
But o/p is opposite to wat I m thinking .
Can anyone correct me If I m wrong
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
I don't think the order is correct.

Since you are using Exception it can catch its sub-class "Arithmeticexception"
So first catch block executes and finally there after.


Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
christian combarel
Ranch Hand

Joined: Aug 04, 2007
Posts: 47
Hi,
I tried this and it runs as expected :

1 : execution of the catch block
2 : execution of the finally block

Here's my output :

java.lang.ArithmeticException: / by zero
at scjp.Main.main(Main.java:18)
Exception

Use your debugger to follow the code execution.



-------------
Chris
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi,

the problem here is that 'e.printStackTrace()' writes to standard error and ' System.out.println' to standard out. Dependent on the environment (e.g. Eclipse), the output from standard out and error may not be printed in the correct order.
 
I agree. Here's the link: http://jrebel.com/download
 
subject: doubt in try catch finally
 
Similar Threads
try/catch
Exception flow control
Please Help with the flow of this code
confusion with finally
doubt in try,catch,finally