aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling 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 "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Swati Thorve
Ranch Hand

Joined: Jan 04, 2006
Posts: 43
Can anybody explain me the o/p of following code

public class Question46{
public static void main(String[] args) throws Exception{
try{
int i=5/(int)Math.random();
System.out.println("i="+i);
} catch(ArithmeticException ae) {
throw new Exception("thrown from first catch");
} catch(Exception e){
throw new Exception("thrown from second catch");
} finally {
return;
}
}
}

options are

A. An exception is thrown and the exception message thrown from first catch is diplayed with the exception stack trace.
B. An exception is thrown and the exception message thrown from second catch is diplayed with the exception stack trace.
C. Prints nothing.
D. Compilation error.
E. Prints i=? where ? stands for the generated random value.


Correct option is C
Plz explain me how?

Bye
-Swati


Thanks,<br />Swati Thorve<br />SCJP 1.4
Narasimhan Madangopalan
Greenhorn

Joined: Sep 30, 2005
Posts: 1
In this code we are dividing by (int)Math.random() this always gives zero(since Math.random() returns a number between 0.0 and 0.99 when this converted to int it always yields zero) and this smoothly terminates because a finally with a return statement can nullify
the exception.

Cheers,
Narasi
Swati Thorve
Ranch Hand

Joined: Jan 04, 2006
Posts: 43
Thanks a lot Narasimhan.
vinod awar
Greenhorn

Joined: Jan 01, 2006
Posts: 7
any exception thrown from a catch block will not be caught in subsequent catch blocks.and while returning return value of finally block hides any previous value
Anju sethi
Ranch Hand

Joined: Dec 26, 2005
Posts: 91
but as per my understanding finally block nullify any previously thrown exception if at all finally block code throws some exception.

But here finally block simply returns the control.how is it nullifying previous exception

Could u please explain.


thanks,<br />Anju Sethi
 
jQuery in Action, 2nd edition
 
subject: Exception Handling