Java サンプル - 例外処理方法
以下の例では、System クラスの System.err.println() を使用して例外の処理方法を示しています:
ExceptionDemo.java ファイル
class ExceptionDemo
{
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 getMessage():My Exception getLocalizedMessage():My Exception toString():java.lang.Exception: My Exception printStackTrace(): java.lang.Exception: My Exception at ExceptionDemo.main(ExceptionDemo.java:5)その他の拡張
Java サンプル