Exceptions should be public

This warning is supported in the stand-alone version of FxCop only. It is not supported in Code Analysis, which is integrated into Visual Studio.

TypeName

ExceptionsShouldBePublic

CheckId

CA1064

Category

Microsoft.Design

Breaking Change

NonBreaking

Cause

A non-public exception derives directly from System.Exception, System.SystemException, or System.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 T:System.Exception, T:System.SystemException, or T:System.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 System.Exception, System.SystemException, or System.ApplicationException.

When to Exclude Warnings

Exclude 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

The following shows three examples which violate the rule.

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)
        {
        }
    }

    // Violates this rule
    [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)
        {
        }
    }

    // Violates this rule
    [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)
        {
        }
    }
}