CA1031 - catch a more specific exception type or rethrow exception

Viewed 1130

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. 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:

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

The proper way would be to use NalpeironException message in diagnostics service. 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.

0 Answers
Related