An exception type that is not a sufficiently specific error from StyleCop VS2010

I have code that throws an exception as follows.

if (jobFinished)
{
   ...                
} else
{                  
    throw new Exception("The server Error ...")
}

It compiles / starts without problems, but when I start StyleCop, I got this error message: Exception is not specific.

Error   10  CA2201 : Microsoft.Usage : Function(string, DirectoryInfo, string, string, string, out string)' 
creates an exception of type 'Exception', an exception type that is not sufficiently 
specific and should never be raised by user code. If this exception instance might be
thrown, use a different exception type.

I just want to throw an error when I come across some error conditions. How can I make a rather specific exception?

+3
source share
2 answers

Throw it away InvalidOperationException.

Although Microsoft has not been consistent in handling exceptions in the past, the current Guidelines for handling exceptions are provided here .

ApplicationException, Exception.

, InvalidOperationException . , Exception.

:

+4

Exception Exception. , , StyleCop

public Class MyException : Exception
{
     public MyException(string errorMsg) : base(errorMessage) {}
}

throw new MyException("blah blah");
0

All Articles