언어

CA1031: 일반적인 예외 유형을 잡지 마십시오.

속성 값
규칙 ID CA1031
제목 일반적인 예외 유형을 catch하지 마세요.
범주 디자인
수정이 중대한 문제를 야기하는지 또는 그렇지 않은지 주요 변경 아님
.NET 10에서 기본적으로 사용하도록 설정 아니요
적용 가능한 언어 C# 및 Visual Basic

원인

System.Exception 또는 System.SystemException와 같은 일반 예외가 catch문에서 catch 되거나 catch()와 같은 일반 catch 절이 사용됩니다.

기본적으로 이 규칙은 catch되는 일반 예외 형식만 플래그하지만 이는 구성 가능합니다.

규칙 설명

일반 예외는 잡으면 안 됩니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 더욱 구체적인 예외를 catch하거나, 일반적인 예외를 catch 블록의 마지막 문으로 다시 throw하세요.

경고를 표시하지 않는 경우

이 규칙의 경고를 억제하지 마십시오. 일반적인 예외 유형을 catch하면 라이브러리 사용자로부터 런타임 문제를 숨길 수 있으며 디버깅이 더 어려워질 수 있습니다.

참고

.NET Framework 4부터 시작해서, CLR(공용 언어 런타임)은 관리 코드에서 처리되어야 하는 Windows의 액세스 위반과 같이 운영 체제 및 관리 코드에서 발생하는 손상된 상태 예외를 더 이상 제공하지 않습니다. .NET Framework 4 또는 이후 버전의 애플리케이션을 컴파일하고 손상된 상태 예외 처리를 유지 관리하려는 경우, 손상된 상태 예외를 처리하는 메서드에 HandleProcessCorruptedStateExceptionsAttribute 특성을 적용해야 합니다.

분석할 코드 구성

다음 옵션을 사용하여 이 규칙이 실행될 코드베이스 부분을 구성합니다.

이 규칙, 적용되는 모든 규칙 또는 적용되는 이 범주(디자인)의 모든 규칙에 대해 이 옵션을 구성할 수 있습니다. 자세한 내용은 코드 품질 규칙 구성 옵션을 참조하세요.

허용되지 않는 예외 유형 이름

특정 예외 유형이 포착되지 않도록 설정할 수 있습니다. 예를 들어 규칙이 catch을 포함하는 NullReferenceException 처리기를 플래그 지정해야 한다고 지정하고 다음 키-값 쌍을 프로젝트의 .editorconfig 파일에 추가합니다.

dotnet_code_quality.CA1031.disallowed_symbol_names = NullReferenceException

옵션 값에 허용되는 형식 이름 서식(|로 구분):

  • 형식 이름만(포함하는 형식 또는 네임스페이스와 관계없이 해당 이름이 있는 모든 기호 포함)
  • 기호의 설명서 ID 형식에 있는 정규화된 이름(T: 접두사 포함)

예:

옵션 값 요약
dotnet_code_quality.CA1031.disallowed_symbol_names = ExceptionType 컴파일에서 'ExceptionType'이라는 모든 기호와 일치합니다.
dotnet_code_quality.CA1031.disallowed_symbol_names = ExceptionType1|ExceptionType2 컴파일에서 'ExceptionType1' 또는 'ExceptionType2'라는 모든 기호와 일치합니다.
dotnet_code_quality.CA1031.disallowed_symbol_names = T:NS.ExceptionType 지정된 정규화된 이름이 있는 ‘ExceptionType’이라는 특정 형식을 찾습니다.
dotnet_code_quality.CA1031.disallowed_symbol_names = T:NS1.ExceptionType1|T:NS1.ExceptionType2 'ExceptionType1' 및 'ExceptionType2'라는 형식을 각각의 정규화된 전체 이름과 일치시킵니다.

이 규칙, 적용되는 모든 규칙 또는 적용되는 이 범주의 모든 규칙(디자인)에 대해 이러한 옵션을 구성할 수 있습니다. 자세한 내용은 코드 품질 규칙 구성 옵션을 참조하세요.

예시

다음 예제에서는 이 규칙을 위반하는 형식과 catch 블록을 올바르게 구현하는 형식을 보여 줍니다.

Imports System
Imports System.IO

Namespace ca1031

    ' Creates two violations of the rule.
    Public Class GenericExceptionsCaught

        Dim inStream As FileStream
        Dim outStream As FileStream

        Sub New(inFile As String, outFile As String)

            Try
                inStream = File.Open(inFile, FileMode.Open)
            Catch ex As SystemException
                Console.WriteLine("Unable to open {0}.", inFile)
            End Try

            Try
                outStream = File.Open(outFile, FileMode.Open)
            Catch
                Console.WriteLine("Unable to open {0}.", outFile)
            End Try

        End Sub

    End Class

    Public Class GenericExceptionsCaughtFixed

        Dim inStream As FileStream
        Dim outStream As FileStream

        Sub New(inFile As String, outFile As String)

            Try
                inStream = File.Open(inFile, FileMode.Open)

                ' Fix the first violation by catching a specific exception.
            Catch ex As FileNotFoundException
                Console.WriteLine("Unable to open {0}.", inFile)
                ' For functionally equivalent code, also catch the
                ' remaining exceptions that may be thrown by File.Open
            End Try

            Try
                outStream = File.Open(outFile, FileMode.Open)

                ' Fix the second violation by re-throwing the generic 
                ' exception at the end of the catch block.
            Catch
                Console.WriteLine("Unable to open {0}.", outFile)
                Throw
            End Try

        End Sub

    End Class

End Namespace
// Creates two violations of the rule.
public class GenericExceptionsCaught
{
    private readonly FileStream? _inStream;
    private readonly FileStream? _outStream;

    public GenericExceptionsCaught(string inFile, string outFile)
    {
        try
        {
            _inStream = File.Open(inFile, FileMode.Open);
        }
        catch (SystemException)
        {
            Console.WriteLine($"Unable to open {inFile}.");
        }

        try
        {
            _outStream = File.Open(outFile, FileMode.Open);
        }
        catch
        {
            Console.WriteLine($"Unable to open {outFile}.");
        }
    }
}

public class GenericExceptionsCaughtFixed
{
    private readonly FileStream? _inStream;
    private readonly FileStream _outStream;

    public GenericExceptionsCaughtFixed(string inFile, string outFile)
    {
        try
        {
            _inStream = File.Open(inFile, FileMode.Open);
        }

        // Fix the first violation by catching a specific exception.
        catch (FileNotFoundException)
        {
            Console.WriteLine($"Unable to open {inFile}.");
        }

        // For functionally equivalent code, also catch 
        // remaining exceptions that may be thrown by File.Open

        try
        {
            _outStream = File.Open(outFile, FileMode.Open);
        }

        // Fix the second violation by rethrowing the generic 
        // exception at the end of the catch block.
        catch
        {
            Console.WriteLine($"Unable to open {outFile}.");
            throw;
        }
    }
}

CA2200: 스택 정보를 유지하도록 다시 던지십시오.