Javaexamples-exception-method

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

Javaの例-例外メソッド

問題の説明

例外メソッドを処理する方法は?

溶液

この例は、SystemクラスのSystem.err.println()メソッドを使用して例外メソッドを処理する方法を示しています。

public class NewClass {
   public static void main(String[] args) {
      try {
         throw new Exception("My Exception");
      } catch (Exception e) {
         System.err.println("Caught Exception");
         System.err.println("getMessage():" + e.getMessage());
         System.err.println("getLocalizedMessage():" + e.getLocalizedMessage());
         System.err.println("toString():" + e);
         System.err.println("printStackTrace():");
         e.printStackTrace();
      }
   }
}

結果

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

Caught Exception
getMassage(): My Exception
gatLocalisedMessage(): My Exception
toString():java.lang.Exception: My Exception
print StackTrace():
   java.lang.Exception: My Exception
   at ExceptionMethods.main(ExceptionMeyhods.java:12)