Javaexamples-exception-getmessage
提供:Dev Guides
Javaの例-例外をスローしながら引数を渡す
問題の説明
チェック例外をスローしながら引数を渡す方法は?
溶液
この例は、例外をスローしながら引数を渡す方法と、ExceptionクラスのgetMessage()メソッドを使用して例外をキャッチしながらこれらの引数を使用する方法を示しています。
public class Main {
public static void main (String args[]) {
try {
throw new Exception("throwing an exception");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
結果
上記のコードサンプルは、次の結果を生成します。
throwing an exception