Aracılığıyla paylaş


CA1064: Özel durumlar genel olmamalıdır

TypeName

ExceptionsShouldBePublic

CheckId

CA1064

Kategori

Microsoft.Design

Değişiklik kesiliyor

Olmayan bölme

Neden

Doğrudan genel olmayan özel durum türeyen Exception, SystemException, veya ApplicationException.

Kural Tanımı

Bir iç özel durum yalnızca kendi iç kapsamı içinde görülebilir.Özel durum iç kapsamı dışında kalan sonra temel exception özel durumu yakalamak için kullanılabilir.İç özel durum alınmış olduğu taktirde Exception, SystemException, veya ApplicationException, harici kod bir durumla yapmanız gerekenler bilmek yeterli bilgi yoktur.

Ancak daha sonra bir iç özel durum için temel olarak kullanılan ortak bir özel durum kodu varsa, daha fazla kod dışarı temel bir durumla akıllı bir şeyler yapmak mümkün olacaktır varsaymak makul.Genel özel durum ne T:System.Exception, T:System.SystemException veya T:System.ApplicationException tarafından sağlanan'den daha fazla bilgi vardır.

İhlalleri düzeltmek nasıl

Özel durum ortak olun ya da iç özel değil genel bir özel durum türetmek Exception, SystemException, veya ApplicationException.

Uyarıları ne zaman

Özel durumun kendi iç kapsamında yakalanan tüm durumlarda eminseniz bu kuralı gelen bir iletiyi bastırmak.

Örnek

Özel durum sınıfı doğrudan özel türeyen ve iç olduğu için bu kural ilk örnek yöntemini, FirstCustomException tetikler.Kural sınıfı ayrıca doğrudan özel türer rağmen sınıf ortak bildirildiğinden SecondCustomException sınıfını etkinleşmez.Doğrudan almıyor çünkü üçüncü sınıf kuralı da başlatılmıyor Exception, SystemException, veya 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)
        {
        }
    }
}