Why is try {…} finally {…} good; try {…} catch{} bad?

后端 未结 20 2376
执笔经年
执笔经年 2020-11-28 01:30

I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn\'t do anything:

StreamReader reader=new  StreamRead         


        
20条回答
  •  日久生厌
    2020-11-28 01:56

    Use Try..Catch..Finally, if your method knows how to handle the exception locally. The Exception occurs in Try, Handled in Catch and after that clean up is done in Finally.

    In case if your method doesn't know how to handle the exception but needs a cleanup once it has occurred use Try..Finally

    By this the exception is propagated to the calling methods and handled if there are any suitable Catch statements in the calling methods.If there are no exception handlers in the current method or any of the calling methods then the application crashes.

    By Try..Finally it is ensured that the local clean up is done before propagating the exception to the calling methods.

提交回复
热议问题