It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exceptions! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exceptions!" Watch "exceptions!" New topic


ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
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?
chin josei

Joined: Jun 22, 2001
Posts: 25
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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
the exception is not supressed if you allow the finally clause to end, just comment return 30;

SCJP2. Please Indent your code using UBB Code
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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
I agree. Here's the link:
subject: exceptions!
It's not a secret anymore!