简体   繁体   English

CA1031-捕获更特定的异常类型或重新抛出异常

[英]CA1031 - catch a more specific exception type or rethrow exception

I understand the code analysis error, but it feels weird why error occurs on this code: 我了解代码分析错误,但是感到奇怪为什么在此代码上发生错误:

public bool IsCurrentLicenseValid()
{     
  int licenseStatusCode = 0;

  try
  {
    int licenseStatusCode = this.GetLicenseStatus();

    if (licenseStatusCode > 0)
    {
      return true;
    }

     return false;
  }
  catch (NalpeironException)
  // catch (NalpeironException ex), but now: the variable ex is declared but never used
  {
    DiagnosticsService.Instance.Trace(
      TraceFilters.Services, 
      "NalpeironLicensingService.IsCurrentLicenseValid", 
      $"License status indicates error '{licenseStatusCode}'");

    // the error goes away if I use ex in the message (i.e. ex.Message)

    return false;
  }
}

I caught specific exception in the catch statement, I just don't want to use exception message or some other exception property. 我在catch语句中捕获了特定的异常 ,我只是不想使用异常消息或其他异常属性。 Is the only solution here to suppress this message? 是这里唯一的抑制此消息的解决方案吗? Or should I use some exception property or method? 还是应该使用一些异常属性或方法?

The NalpeironException extends Exception, here's the code: NalpeironException扩展了Exception,这是代码:

public class NalpeironException : Exception
{
    private Enum errorId;

    public NalpeironException(Enum errorId, string message, Exception inner) : base(message, inner)
    {
        this.errorId = errorId;
    }

    public Enum ErrorId
    {
        get { return this.errorId; }
        set { this.errorId = value; }
    }

    public bool IsEqualId(Enum errorId)
    {
        return this.ErrorId.GetType() == errorId.GetType() && this.ErrorId.Equals(errorId);
    }
}

UPDATE UPDATE

The proper way would be to use NalpeironException message in diagnostics service. 正确的方法是在诊断服务中使用NalpeironException消息。 In order words to log the exception message. 为了文字记录异常信息。 I'm afraid I can't do that because the code was already written by someone else and the application was shipped out. 恐怕我做不到,因为代码已经由其他人编写并且应用程序已经发货。

I think your code has no meaning, just use a more specific type of exception instead of creating your own type of exception ( NalpeironException ), for that you have to analyze your code and extract the exceptions that can be caused by your code, it is also preferable to use exception ( System.Exception ) types already existing in the .Net. 我认为您的代码没有任何意义,只是使用一种更具体的异常类型而不是创建自己的异常类型( NalpeironException ),因为您必须分析代码并提取可能由代码引起的异常,这是还建议使用.Net中已经存在的异常( System.Exception )类型。

You will need to use your own type of exception only for more specific needs (Business Logic, Business Rules, Exception Managment , ....) 您仅需要将自己的异常类型用于更具体的需求(业务逻辑,业务规则,异常管理等)。

Check out the link below it could be helpful : https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/using-standard-exception-types 查看下面的链接可能会有所帮助: https : //docs.microsoft.com/zh-cn/dotnet/standard/design-guidelines/using-standard-exception-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM