• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling Question from Dan Chisholm's mock exam

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the question below, I am somehow missing the reason why the exception thrown by m5 (method 5) is not getting caught. The strange part is I changed the throws clause in the signature to "ColorException" and it still didn't catch it.
I know its something obvious but I just can't seem to put my finger on the spot.. Any help would be appreciated.
class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
void m1() throws Exception {throw new ColorException();}
void m2() throws ColorException {throw new WhiteException();}
void m3() throws WhiteException {}
void m4() throws Exception {throw new Exception();}
void m5() throws WhiteException {throw new WhiteException();}
void m6() {}
public static void main (String[] args) {
White white = new White();
char a,b,c,d,f,g,h,i;
a = b = c = d = f = g = h = i = 'a';
try {
try {white.m1();} catch (ColorException e) {a++;} h++;
try {white.m2();} catch (ColorException e) {b++;} h++;
try {white.m3();} catch (ColorException e) {c++;} h++;
try {white.m4();} finally {d++;}
try {white.m5();} catch (ColorException e) {f++;} finally {h++;}
try {white.m6();} catch (Exception e) {g++;} finally {h++;}
} catch (Exception e) {i++;}
System.out.println(a+","+b+","+c+","+d+","+f+","+g+","+h+","+i);
}
}

What is the result of attempting to compile and run the above program?
a. Prints: b,b,a,b,a,a,d,b
b. Prints: b,b,a,b,a,a,f,b
c. Prints: b,a,a,a,a,a,a,b
d. Prints: b,a,a,b,a,a,c,b
e. Prints: b,b,a,a,b,a,f,b
f. Prints: b,b,b,a,b,b,f,b
g. Runtime Exception
h. Compiler Error
i. None of the Above
The answer is a
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that you have try blocks inside the "main" try block, but not all of them have a corresponding catch block. In your "main" try block

m4() throws an exception which is not caught by an "inner" catch block. The thread of execution is now therefore interrupted and the error propagates outward, and is caught by the "main" catch block. Methods m5() and m6() are not called.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic