例外処理について学習していますが、今質問があります。
私はこのコードの結果は345だと思いますが、なぜ結果が35であるのか分かりません。
例外が発生しても、コードSystem.out.println(4)を実行するべきではありませんか? method1()
であなただけの別の例外であるArithmethicException
をキャッチするのに対し、method2()
NullPointerException
で例外処理メソッド終了なしで終了する
public class Six {
public static void main(String[] args) {
try {
method1();
} catch(Exception e) {
System.out.println(5);
}
}
static void method1() {
try {
method2();
System.out.println(1);
} catch(ArithmeticException e) {
System.out.println(2);
} finally {
System.out.println(3);
}
System.out.println(4);
}
static void method2() {
throw new NullPointerException();
}
}
NullPointerException例外がcatchブロックによって処理されないため、System.out.println(4);が例外です。ステートメントが実行されていない –
私は愚かな間違いを持って、今私は理解する!ありがとうございました :) –