Javaexamples-exception-hierarchy

提供:Dev Guides
2020年6月22日 (月) 17:29時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

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