try{} finally{} construct with return values
This question already has an answer here:
Everything works exactly as expected, no bugs here. When you have doubts, the JLS is your savior:
JLS - 14.20.2. Execution of try-finally and try-catch-finally :
If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and then there is a choice:
If the finally block completes normally, then the try statement
completes abruptly for reason R.
If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is
discarded).
It overrides the value in the try block.
return inside finally discards all exceptions that can be thrown in try clause.
上一篇: 在java异常处理中返回语句
