• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exceptions!

 
ashok khetan
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • 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.
 
chin josei
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the exception is not supressed if you allow the finally clause to end, just comment return 30;
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic