Java Try Catch Finally blocks without Catch

后端 未结 11 784
没有蜡笔的小新
没有蜡笔的小新 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:14

    how does the try block work if it encounters an exception or anything throwable

    The exception is thrown out of the block, just as in any other case where it's not caught.

    The finally block is executed regardless of how the try block is exited -- regardless whether there are any catches at all, regardless of whether there is a matching catch.

    The catch blocks and the finally are orthogonal parts of the try block. You can have either or both. With Java 7, you'll be able to have neither!

提交回复
热议问题