Java例外処理(try、catch、finally使用)
コードを直接入力し、呼び出しが必要なメソッドを次のように貼ります.
1.Exceptionを投げ出して、finallyがなくて、catchがreturnに出会う時
バックグラウンド出力結果:
2.Exceptionを放出し、catch体にreturnがある場合、finally体のコードブロックはcatchがreturnを実行する前に実行される
バックグラウンド出力結果:
3.Exceptionを投げ出さないで、finallyコードブロックの中でreturnに出会って、finallyが実行した後に全体の方法を終了します
バックグラウンド出力結果:
4.Exceptionを捨てないで、finallyコードブロックの中でSystemに出会う.exit()メソッドは、メソッドだけでなくプログラム全体を終了および終了します.
バックグラウンド出力結果:
5.Exceptionを放出し、catchとfinallyが同時にreturnに遭遇した場合、catchのreturn戻り値は戻されず、finallyのreturn文はメソッド全体を終了して戻ります
バックグラウンド出力結果:
6.Exceptionを投げ出さず、finallyがreturnに遭遇した場合、tryのreturn戻り値は返されず、finallyのreturn文はメソッド全体を終了して戻ります
バックグラウンド出力結果:
結論
Javaの例外処理では,プログラムがtryの中のコードブロックを実行した後,このメソッドはすぐに終了するのではなく,finallyのコードブロックがあるかどうかを探し続けた. finallyコードブロックがない場合、tryコードブロックを実行した後、メソッド 全体を終了するために、メソッド全体が対応する値を返す. finallyコードブロックがある場合、プログラムがtryコードブロックのreturnの文に実行するとすぐにreturnを実行するのではなく、finallyコードブロックのコード を先に実行する.
finallyコードブロックにreturnがないか、プログラムを終了できるコードがない場合、プログラムはfinallyコードブロックコードを実行した後、tryコードブロックに戻ってreturn文を実行してメソッド全体を終了します.finallyコードブロックにreturnまたはプログラムを終了できるコードが含まれている場合、メソッドはfinallyを実行した後に終了し、tryコードブロックに戻ってreturnを実行しません.
異常を投げ出す場合、原理も上と同じですから、上記のtryをcatchに変えて理解すればOKです.
public static void enterTryMethod() {
System.out.println("enter after try field");
}
public static void enterExceptionMethod() {
System.out.println("enter catch field");
}
public static void enterFinallyMethod() {
System.out.println("enter finally method");
}
1.Exceptionを投げ出して、finallyがなくて、catchがreturnに出会う時
public static int catchTest() {
int res = 0;
try {
res = 10 / 0; // Exception,
enterTryMethod();
return res; // Exception ,
} catch (Exception e) {
enterExceptionMethod();
return 1; // Exception ,
}
}
バックグラウンド出力結果:
enter catch field
1
2.Exceptionを放出し、catch体にreturnがある場合、finally体のコードブロックはcatchがreturnを実行する前に実行される
public static int catchTest() {
int res = 0;
try {
res = 10 / 0; // Exception,
enterTryMethod();
return res; // Exception ,
} catch (Exception e) {
enterExceptionMethod();
return 1; // Exception ,
} finally {
enterFinallyMethod(); // Exception ,finally catch return
}
}
バックグラウンド出力結果:
enter catch field
enter finally method
1
3.Exceptionを投げ出さないで、finallyコードブロックの中でreturnに出会って、finallyが実行した後に全体の方法を終了します
public static int catchTest() {
int res = 0;
try {
res = 10 / 2; // Exception
enterTryMethod();
return res; // , finally
} catch (Exception e) {
enterExceptionMethod();
return 1;
} finally {
enterFinallyMethod();
return 1000; // finally return , return , try catch ,
}
}
バックグラウンド出力結果:
enter after try field
enter finally method
1000
4.Exceptionを捨てないで、finallyコードブロックの中でSystemに出会う.exit()メソッドは、メソッドだけでなくプログラム全体を終了および終了します.
public static int catchTest() {
int res = 0;
try {
res = 10 / 2; // Exception
enterTryMethod();
return res; // , finally ,
} catch (Exception e) {
enterExceptionMethod();
return 1;
} finally {
enterFinallyMethod();
System.exit(0); // finally System.exit() ,System.exit() ,
}
}
バックグラウンド出力結果:
enter after try field
enter finally method
5.Exceptionを放出し、catchとfinallyが同時にreturnに遭遇した場合、catchのreturn戻り値は戻されず、finallyのreturn文はメソッド全体を終了して戻ります
public static int catchTest() {
int res = 0;
try {
res = 10 / 0; // Exception,
enterTryMethod();
return res; // Exception ,
} catch (Exception e) {
enterExceptionMethod();
return 1; // Exception , , finally
} finally {
enterFinallyMethod();
return 10; // return , 10
}
}
バックグラウンド出力結果:
enter catch field
enter finally method
10
6.Exceptionを投げ出さず、finallyがreturnに遭遇した場合、tryのreturn戻り値は返されず、finallyのreturn文はメソッド全体を終了して戻ります
public static int catchTest() {
int res = 0;
try {
res = 10 / 2; // Exception
enterTryMethod();
return res; // , finally
} catch (Exception e) {
enterExceptionMethod();
return 1;
} finally {
enterFinallyMethod();
return 10; // return , 10
}
}
バックグラウンド出力結果:
enter after try field
enter finally method
10
結論
Javaの例外処理では,プログラムがtryの中のコードブロックを実行した後,このメソッドはすぐに終了するのではなく,finallyのコードブロックがあるかどうかを探し続けた.
finallyコードブロックにreturnがないか、プログラムを終了できるコードがない場合、プログラムはfinallyコードブロックコードを実行した後、tryコードブロックに戻ってreturn文を実行してメソッド全体を終了します.finallyコードブロックにreturnまたはプログラムを終了できるコードが含まれている場合、メソッドはfinallyを実行した後に終了し、tryコードブロックに戻ってreturnを実行しません.
異常を投げ出す場合、原理も上と同じですから、上記のtryをcatchに変えて理解すればOKです.