Problema 12
class Exceptie1 extends Exception{}
class Exceptie2 extends Exceptie1{}
class Exceptie3 extends Exceptie1{}
class C{
public void oMetoda() throws Exceptie1, Exceptie2, Exceptie3 {
throw new Exceptie2();
}
}
public class Main {
public static void main(String[] args) {
try {
C c = new C();
c.oMetoda();
}
catch(Exceptie1 e1) {
System.out.println("Exceptie1");
}
}
}
Stergem celelalte "catch-uri" pentru ca exceptia a fost prinsa. Exceptie2 este un tip de Exceptie1, iar Exceptie1 a fost prins. Orice Exceptie2 si Exceptie3 este un fel de Exceptie1.