Can somebody explain why the result comes out this way.
class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
public static void main(
String args[]) {
int a, b, c, d, f;
a = b = c = d = f = 0;
int x = 1;
try {
switch (x) {
case 1: throw new Level1Exception();
case 2: throw new Level2Exception();
case 3: throw new Level3Exception();
}
a++;
}
catch (Level3Exception e) { b++;}
catch (Level2Exception e) { c++;}
catch (Level1Exception e) { d++;} // Doubt
finally {f++;}
System.out.print(a+","+b+","+c+","+d+","+f);
}
}
Answer is 0,0,0,1,1
My question is why is that only the catch(Level1Exception e) is handling inspite of the case 2 and 3 statements throwing Level2Exception and Level3Exception respectively. Is it because Level1Exception is the super class?