Why not catch general Exceptions

后端 未结 12 1676
鱼传尺愫
鱼传尺愫 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

    Since your warning message shows that this is in Main(), I'll assume that in lower levels, you do catch only more specific Exceptions.

    For Main(), I'd consider two cases:

    1. Your own (debugging) build, where you want all the exception information you can get: Do not catch any Exceptions here, so the debugger breaks and you have your call stack,
    2. Your public releases, where you want the application to behave normally: Catch Exception and display a nice message. This is always better (for the average user) than the 'send report' window.

    To do this nicely, just check if DEBUG is defined (and define it, if VS doesn't do this automatically):

    #if DEBUG
      yadda(); // Check only specific Exception types here
    #else
      try
      {
        yadda();
      }
      catch (Exception e)
      {
         ShowMessage(e); // Show friendly message to user
      }
    #endif
    

    I'd disable the warning about catching general Exceptions, but only for your Main() function, catching Exception in any other method is unwise, as other posters have said already.

提交回复
热议问题