Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from Dan Mock Exam

 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
abstract void m1() throws ColorException;
}
class White extends Color {
void m1() throws WhiteException {throw new WhiteException();}
public static void main (String[] args) {
White white = new White();
int a,b,c,d;
a = b = c = 0;
try {
white.m1();
a++;
} catch (WhiteException e) {b++;}
finally {c++;}
System.out.print(a+","+b+","+c);
}
}
Step by step Analysis, Please correct me.
-----------------------------------------
class ColorException extends Exception {}
ColorException is subclass of Excpetion
class WhiteException extends ColorException {}
WhiteExcpetion is subclass of ColorException
abstract class Color {
abstract void m1() throws ColorException;
What is going on here? Please explain.Is the method m1() going to use ColorException class?
}
class White extends Color {
White is subclass of Color
void m1() throws WhiteException {throw new WhiteException();
What is going on here? Please explain. Is method m1()goint to use some oneWhiteException?
}
public static void main (String[] args) {
White white = new White();
int a,b,c,d;
a = b = c = 0;
try {
white.m1();
which method will get called?
1.abstract void m1() or
2. void m1() Please explain
from down I know whats going on
a++;
} catch (WhiteException e) {b++;}
finally {c++;}
System.out.print(a+","+b+","+c);
}
}

I really appreciate. Thanks.
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ August 22, 2002: Message edited by: zarina mohammad ]
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it going to through exception? Any takers?
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void m1() throws WhiteException {throw new WhiteException();}
class White cannot through WhiteException?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that Zarina's post demonstrates that White.m1 throws an Exception and that causes the "catch" clause to be processed.

[ August 22, 2002: Message edited by: Dan Chisholm ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic