いくつかの例外をキャッチする方法?


この記事では、いくつかの例外をキャッチする方法と、正しい順序でそれらを記述する方法を見ていきます.
いくつかの例外をキャッチ
いくつかの例外に対処する方法を理解する例を挙げましょう.
class Example {
   public static void main (String args []) {
      try{
         int arr [] = new int [7];
         arr [4] = 30/0;
         System.out.println ( "Last Statement try block");
      }
      catch (ArithmeticException e) {
         System.out.println ( "You do not have to divide a number by zero");
      }
      catch (ArrayIndexOutOfBoundsException e) {
         System.out.println ( "Accessing array elements beyond the bounds");
      }
      catch (Exception e) {
         System.out.println ( "Some other Exception");
      }
      System.out.println ( "Out of the try-catch block");
   }
}

出力
番号をゼロに分ける必要はありません
トライキャッチブロックから
上の例では、tryブロックの算術演算例外に対する最初のcatchブロックを実行します.
では、コードを少し変更して出力の変更を見ましょう.
class Example {
   public static void main (String args []) {
      try{
         int arr [] = new int [7];
         arr [10] = 10/5;
         System.out.println ( "Last Statement try block");
      }
      catch (ArithmeticException e) {
         System.out.println ( "You do not have to divide a number by zero");
      }
      catch (ArrayIndexOutOfBoundsException e) {
         System.out.println ( "Accessing array elements beyond the bounds");
      }
      catch (Exception e) {
         System.out.println ( "Some other Exception");
      }
      System.out.println ( "Out of the try-catch block");
   }
}

Read more