Java Try Catch Finally blocks without Catch

后端 未结 11 797
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 20:38

I\'m reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception

11条回答
  •  半阙折子戏
    2020-11-28 21:04

    The Java Language Specification(1) describes how try-catch-finally is executed. Having no catch is equivalent to not having a catch able to catch the given Throwable.

    • If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
      • If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then …
      • If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
        • If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
        • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).

    (1) Execution of try-catch-finally

提交回复
热议问题