try{} finally{} construct with return values

This question already has an answer here:

  • Java try-finally return design question 7 answers
  • Does finally always execute in Java? 50 answers

  • 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.

    链接地址: http://www.djcxy.com/p/73700.html

    上一篇: 在java异常处理中返回语句

    下一篇: 尝试{} finally {}用返回值构造