| Author |
Exception Question from Dan Chisholm
|
Purvesh Vora
Ranch Hand
Joined: Dec 10, 2003
Posts: 47
|
|
Hi, There is one exception question from Dan Chisholm. class ColorException extends Exception {} class WhiteException extends ColorException {} class White { void m1() throws ColorException {throw new ColorException();} void m2() throws WhiteException {throw new 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 (WhiteException e) {b++;} try {white.m2(); d++;} catch (WhiteException e) {f++;} System.out.print(a+","+b+","+d+","+f); }} What is the result of attempting to compile and run the program? a. Prints: 0,1,0,0 b. Prints: 1,1,0,1 c. Prints: 0,1,0,1 d. Prints: 0,1,1,1 e. Prints: 1,1,1,1 f. Compile-time error g. Run-time error h. None of the above The answer is f. Can anybody plese explain the same. Thanks & Regards, Purvesh Vora ------------------ IBM Test 486(OOAD With UML)
|
Regards,<br />Purvesh<br />SCJP, SCBCD, IBM Exam #486
|
 |
Anupreet Arora
Ranch Hand
Joined: Jun 17, 2003
Posts: 81
|
|
The answer F is correct and the program will fail during compilation in the first try-catch block inside the main method of the program. Inside the try block, there is a call to method m1 on white object. This method throws a ColorException, which is not handled by the catch block. The catch block handles WhiteException which is a subclass of ColorException. So the program will not compile.
|
 |
Ko Ko Naing
Ranch Hand
Joined: Jun 08, 2002
Posts: 3178
|
|
Here is the corrected code... I just made a change to the bold word..... The thrown exception must be caught by the same exception catcher (or) its superclass... Hope it's clear...
|
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
|
 |
Seshachala Dharmaveer Kalluri
Greenhorn
Joined: Dec 16, 2003
Posts: 2
|
|
Originally posted by Purvesh Vora: Hi, There is one exception question from Dan Chisholm. class ColorException extends Exception {} class WhiteException extends ColorException {} class White { void m1() throws ColorException {throw new ColorException();} void m2() throws WhiteException {throw new 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 (WhiteException e) {b++;} try {white.m2(); d++;} catch (WhiteException e) {f++;} System.out.print(a+","+b+","+d+","+f); }} What is the result of attempting to compile and run the program? a. Prints: 0,1,0,0 b. Prints: 1,1,0,1 c. Prints: 0,1,0,1 d. Prints: 0,1,1,1 e. Prints: 1,1,1,1 f. Compile-time error g. Run-time error h. None of the above The answer is f. Can anybody plese explain the same. Thanks & Regards, Purvesh Vora ------------------ IBM Test 486(OOAD With UML)
Yes the correct answer is f, because in the first try block of the main method, the method call White.m1() is throwing an exception of class ColorException, which is being caught by an object of its subclass WhiteException, which cannot be done.
|
 |
Purvesh Vora
Ranch Hand
Joined: Dec 10, 2003
Posts: 47
|
|
Got it. Thanks Friends. Best Reagards Purvesh
|
 |
 |
|
|
subject: Exception Question from Dan Chisholm
|
|
|