28 lines
607 B
Java
28 lines
607 B
Java
|
//: exceptions/Human.java
|
||
|
// Catching exception hierarchies.
|
||
|
|
||
|
class Annoyance extends Exception {}
|
||
|
class Sneeze extends Annoyance {}
|
||
|
|
||
|
public class Human {
|
||
|
public static void main(String[] args) {
|
||
|
// Catch the exact type:
|
||
|
try {
|
||
|
throw new Sneeze();
|
||
|
} catch(Sneeze s) {
|
||
|
System.out.println("Caught Sneeze");
|
||
|
} catch(Annoyance a) {
|
||
|
System.out.println("Caught Annoyance");
|
||
|
}
|
||
|
// Catch the base type:
|
||
|
try {
|
||
|
throw new Sneeze();
|
||
|
} catch(Annoyance a) {
|
||
|
System.out.println("Caught Annoyance");
|
||
|
}
|
||
|
}
|
||
|
} /* Output:
|
||
|
Caught Sneeze
|
||
|
Caught Annoyance
|
||
|
*///:~
|