不要在 finally 语句块中使用 return

因为:

If the try clause executes a return, the compiled code does the following:

  1. Saves the return value (if any) in a local variable.
  2. Executes a jsr to the code for the finally clause.
  3. Upon return from the finally clause, returns the value saved in the local variable.

意味着当在 try 中 return 时,并不会立即执行 return,而是将返回值先存着一个本地变量中,等 finally 执行完之后才将本地变量给返回。

所以,当 try 语句和 finally 语句中都有 return 语句时,try 语句块中的 return 语句会被忽略。

你可能感兴趣的:(Java,java)