This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Question from Dan Chisholm

 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Seshachala Dharmaveer Kalluri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.
Thanks Friends.
Best Reagards
Purvesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic