📜  Java |异常处理问题4

📅  最后修改于: 2021-06-29 04:20:37             🧑  作者: Mango

class Base extends Exception {}
class Derived extends Base  {}
  
public class Main {
  public static void main(String args[]) {
   // some other stuff
   try {
       // Some monitored code
       throw new Derived();
    }
    catch(Base b)     { 
       System.out.println("Caught base class exception"); 
    }
    catch(Derived d)  { 
       System.out.println("Caught derived class exception"); 
    }
  }
} 

(A)被发现的基类例外
(B)捕获派生类异常
(C)编译器错误,因为派生不可抛出
(D)编译器错误,因为在派生类之前捕获了基类异常答案: (D)
说明:请参见将基类和派生类捕获为异常

以下是下面程序中的错误

Main.java:12: error: exception Derived has already been caught
    catch(Derived d)  { System.out.println("Caught derived class exception"); } 

这个问题的测验