File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on catching exception on following code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on catching exception on following code" Watch "doubt on catching exception on following code" New topic

doubt on catching exception on following code

R .sourav nayak
Ranch Hand

Joined: May 14, 2006
Posts: 67
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws ColorException {throw new WhiteException();}
void m2() throws WhiteException {}
public static void main (String[] args) {
White white = new White();
int a,b,d,f; a = b = d = f = 0;
try {white.m1(); a++;} catch (ColorException e) {b++;}
try {white.m2(); d++;} catch (WhiteException e) {f++;}

What is the result of attempting to compile and run the program?

a. Prints: 0,1,0,0
b. Prints: 1,1,0,0
c. Prints: 0,1,1,0
d. Prints: 1,1,1,0
e. Prints: 1,1,1,1
f. Compile-time error
g. Run-time error
h. None of the above

answer c.

why exception is not cought for method m2()?

thanks in advance.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
void m2() throws WhiteException {} does not throw any exceptions.
subject: doubt on catching exception on following code
Similar Threads
Exception logic.
Q5 from Dan's top exam (2nd)
Exception Handling Question from Dan Chisholm's mock exam
Exception Question from Dan Chisholm
An Exception question in Dan's mock exam