Share via


CA1064:例外は public として設定する必要があります

プロパティ
ルール ID CA1064
Title 例外は public として設定する必要があります
[カテゴリ] デザイン
修正が中断ありか中断なしか なし
.NET 8 では既定で有効 いいえ

原因

パブリック以外の例外は、ExceptionSystemExceptionApplicationException のいずれかから直接派生します。

規則の説明

内部例外は、その内部スコープ内でのみ認識されます。 内部スコープの外側にある例外は、基本例外を使用しなければキャッチできません。 内部例外が ExceptionSystemException、または ApplicationException を継承している場合、外部コードはその例外の処理に関する十分な情報を取得できません。

ただし、後で内部例外のベースとして使用されるパブリック例外がコードに含まれている場合は、基本例外を使用したインテリジェントな処理が、さらにコードによって実行されると想定することは理にかなっています。 パブリック例外には、ExceptionSystemException、または ApplicationException で提供されるものよりも多くの情報が含まれます。

違反の修正方法

例外をパブリックにするか、ExceptionSystemExceptionApplicationException 以外のパブリック例外から内部例外を派生させます。

どのようなときに警告を抑制するか

プライベート例外がそれ自体の内部スコープ内でキャッチされることがあらゆる場合に確実なときは、この規則からのメッセージを表示しないようにします。

警告を抑制する

単一の違反を抑制するだけの場合は、ソース ファイルにプリプロセッサ ディレクティブを追加して無効にしてから、規則をもう一度有効にします。

#pragma warning disable CA1064
// The code that's violating the rule is on this line.
#pragma warning restore CA1064

ファイル、フォルダー、またはプロジェクトの規則を無効にするには、構成ファイルでその重要度を none に設定します。

[*.{cs,vb}]
dotnet_diagnostic.CA1064.severity = none

詳細については、「コード分析の警告を抑制する方法」を参照してください。

この規則は最初の例のメソッド FirstCustomException に適用されます。例外クラスが例外から直接派生し、内部であるためです。 この規則は、SecondCustomException クラスには適用されません。このクラスも例外から直接派生していますが、クラスがパブリックとしてとして宣言されているからです。 3 番目のクラスも、System.ExceptionSystem.SystemExceptionSystem.ApplicationException のいずれかから直接派生していないため、規則は適用されません。

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