다음을 통해 공유


CA1064: 예외는 public이어야 합니다.

속성
규칙 ID CA1064
제목 예외는 public이어야 합니다.
범주 디자인
수정 사항이 호환성을 깨뜨리는지 여부 또는 무중단인지 여부 주요 변경 아님
.NET 10에서 기본적으로 사용하도록 설정 아니요
적용 가능한 언어 C# 및 Visual Basic

원인

public이 아닌 예외는 Exception, SystemException 또는 ApplicationException에서 직접 파생됩니다.

규칙 설명

내부 예외는 내부 범위 내에만 표시됩니다. 예외가 내부 범위 밖에 놓이게 되면 예외를 catch하는 데 기본 예외만 사용할 수 있습니다. 내부 예외가 Exception, SystemException, 또는 ApplicationException로부터 상속될 경우, 외부 코드는 예외를 처리하는 방법에 대한 충분한 정보를 제공받지 못합니다.

그러나 나중에 내부 예외에 대한 기준으로 사용되는 public 예외가 코드에 있는 경우 범위를 더 벗어난 코드는 기본 예외를 통해 인텔리전트 작업을 수행할 수 있다고 가정하는 것이 합리적입니다. public 예외에는 Exception, SystemException 또는 ApplicationException에서 제공하는 것보다 더 많은 정보가 포함됩니다.

위반 문제를 해결하는 방법

예외를 public으로 설정하거나 Exception, SystemException 또는 ApplicationException이 아닌 public 예외에서 내부 예외를 파생시킵니다.

경고를 표시하지 않는 경우

모든 경우에 private 예외가 자체 내부 범위 내에서 catch될 것으로 확신하면 해당 규칙에서 메시지를 표시하지 않습니다.

경고 표시 안 함

단일 위반을 억제하려면 원본 파일에 전처리기 지시문을 추가하여 규칙을 비활성화한 후 다시 활성화하십시오.

#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 클래스는 Exception 클래스에서 직접 파생되지만, 클래스가 public으로 선언되어 있기 때문에 룰이 작동하지 않습니다. 또한 세 번째 클래스는 System.Exception, System.SystemException 또는 System.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)
    {
    }
}