CA1064: Exceptions should be public
Note
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Item | Value |
---|---|
TypeName | ExceptionsShouldBePublic |
CheckId | CA1064 |
Category | Microsoft.Design |
Breaking Change | Non Breaking |
Cause
A non-public exception derives directly from Exception, SystemException, or ApplicationException.
Rule Description
An internal exception is only visible inside its own internal scope. After the exception falls outside the internal scope, only the base exception can be used to catch the exception. If the internal exception is inherited from Exception, SystemException, or ApplicationException, the external code will not have sufficient information to know what to do with the exception.
But, if the code has a public exception that later is used as the base for a internal exception, it is reasonable to assume the code further out will be able to do something intelligent with the base exception. The public exception will have more information than what is provided by T:System.Exception, T:System.SystemException, or T:System.ApplicationException.
How to Fix Violations
Make the exception public, or derive the internal exception from a public exception that is not Exception, SystemException, or ApplicationException.
When to Suppress Warnings
Suppress a message from this rule if you are sure in all cases that the private exception will be caught within its own internal scope.
Example
This rule fires on the first example method, FirstCustomException because the exception class derives directly from Exception and is internal. The rule does not fire on the SecondCustomException class because although the class also derives directly from Exception, the class is declared public. The third class also does not fire the rule because it does not derive directly from System.Exception, System.SystemException, or System.ApplicationException.
using System;
using System.Runtime.Serialization;
namespace Samples
{
// Violates this rule
[Serializable]
internal class FirstCustomException : Exception
{
internal FirstCustomException()
{
}
internal FirstCustomException(string message)
: base(message)
{
}
internal FirstCustomException(string message, Exception innerException)
: base(message, innerException)
{
}
protected FirstCustomException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
// Does not violate this rule because
// SecondCustomException is public
[Serializable]
public class SecondCustomException : Exception
{
public SecondCustomException()
{
}
public SecondCustomException(string message)
: base(message)
{
}
public SecondCustomException(string message, Exception innerException)
: base(message, innerException)
{
}
protected SecondCustomException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
// Does not violate this rule because
// ThirdCustomException it does not derive directly from
// Exception, SystemException, or ApplicationException
[Serializable]
internal class ThirdCustomException : SecondCustomException
{
internal ThirdCustomException()
{
}
internal ThirdCustomException(string message)
: base(message)
{
}
internal ThirdCustomException(string message, Exception innerException)
: base(message, innerException)
{
}
protected ThirdCustomException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}