Javaexamples-exception-hierarchy

提供:Dev Guides
移動先:案内検索

Javaの例-例外階層

問題の説明

例外階層を処理する方法は?

例外階層のサンプルダイアグラムを次に示します。

image

溶液

この例は、Exceptionクラスを拡張して例外階層を処理する方法を示しています。

class Animal extends Exception {
}
class Mammel extends Animal {
}
public class Human {
   public static void main(String[] args) {
      try {
         throw new Mammel();
      } catch (Mammel m) {
         System.err.println("It is mammel");
      }
   }
}

結果

上記のコードサンプルは、次の結果を生成します。

It is mammel