Why not catch general Exceptions

后端 未结 12 1679
鱼传尺愫
鱼传尺愫 2020-11-27 16:26

My VS just told me;

Warning 2 CA1031 : Microsoft.Design : Modify \'Program.Main(string[])\' to catch a more specific exception than \'Exception\' or r

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 16:57

    I am all for catching specific known exceptions and handling state...but I use general catch exceptions to quickly localize problems and pass errors up to calling methods which handle state just fine. During development as those are caught, they have a place right next to the general exception and are handled once in release.

    I believe one should attempt to remove these once the code goes into production, but to constantly be nagged during the initial code creation is a bit much.

    Hence turn off (uncheck) the warning by the project settings as found in Microsoft.CodeQuality.Analyzers. That is found in the project settings under Code Analysis:

提交回复
热议问题