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

后端 未结 20 2388
执笔经年
执笔经年 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:57

    If you don't know what exception type to catch or what to do with it, there's no point in having a catch statement. You should just leave it for a higher-up caller that may have more information about the situation to know what to do.

    You should still have a finally statement in there in case there is an exception, so that you can clean up resources before that exception is thrown to the caller.

提交回复
热议问题