1. ホーム
  2. ジャワ

finally ブロックが正常に終了しない」場合に使用します。

2022-03-18 01:29:09

Eclipseでtry,catch,finallyを使用する際の注意点

が書いています。
最終ブロックが正常に完了しない

 理由を教えてください。

1. the finally block is executed regardless of whether there is a return statement in the try block or catch block.
2. The return statement in the finally block will override the previous return statement (return statement in the try block or catch block), so if there is a return statement in the finally block, the Eclipse compiler will report a warning "finally block does not complete normally".
3. If the finally block contains a return statement, even if the previous catch block re-throws an exception, the statement calling the method will not get the exception re-thrown by the catch block, but will get the return value of the finally block, and will not catch the exception.

 そのため、finallyの中でreturnを使わないようにしましょう。