• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exceptions!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class MyClass {
int show() {
try {
int k = 20/0;
return 10;
} catch(Exception e) {
int k = 10/0;
return 20;
}
finally {
System.out.println("in finally");
return 30;
}
}
public static void main(String args[]) {
System.out.println("In main");
System.out.println(new MyClass().show());
}
}
Hi! in the above program after executing the finally block, the exception thrown by the catch block is not handled by the default handler or it is not passed to the main method, why?
ashok.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hii..
If a finally block is specified after the catch block,then
regardless of whether the catch block itself throws an
exception,finally block is executed.Hence whatever exception
that is thrown in catch block will be suppressed.
Just comment the finally block..
The program will throws ArithmeticException
Hope this helps
chin
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the exception is not supressed if you allow the finally clause to end, just comment return 30;
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
throwing the exception messsage is preceded by the return in the
finally clause
This happens in both exceptions wheather thrown in the try block or in the catch block
 
reply
    Bookmark Topic Watch Topic
  • New Topic