• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

doubt in try catch finally

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic