Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

CA1064: Exceptions should be public

TypeName

ExceptionsShouldBePublic

CheckId

CA1064

Category

Microsoft.Design

Breaking Change

Non Breaking

A non-public exception derives directly from Exception, SystemException, or ApplicationException.

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.

Make the exception public, or derive the internal exception from a public exception that is not Exception, SystemException, or ApplicationException.

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.

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


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.