Java Try Catch Finally blocks without Catch

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

    Inside try block we write codes that can throw an exception. The catch block is where we handle the exception. The finally block is always executed no matter whether exception occurs or not.

    Now if we have try-finally block instead of try-catch-finally block then the exception will not be handled and after the try block instead of control going to catch block it will go to finally block. We can use try-finally block when we want to do nothing with the exception.

提交回复
热议问题