CA1031:不要攔截一般例外狀況類型

屬性
規則識別碼 CA1031
標題 不要攔截一般例外狀況類型
類別 設計
修正程式是中斷或非中斷 不中斷
預設在 .NET 8 中啟用 No

原因

例如 或 System.SystemException 之類的System.Exceptioncatch般例外狀況在 語句中攔截,或使用之類的catch()一般 catch 子句。

根據預設,此規則只會標幟要攔截的一般例外狀況類型,但這是可設定

檔案描述

不應該攔截一般例外狀況。

如何修正違規

若要修正此規則的違規,請攔截更特定的例外狀況,或重新擲回一般例外狀況作為區塊中的 catch 最後一個語句。

隱藏警告的時機

請勿隱藏此規則的警告。 擷取一般例外狀況類型可能會隱藏連結庫用戶的運行時間問題,並讓偵錯更加困難。

注意

從 .NET Framework 4 開始,Common Language Runtime (CLR) 不再提供操作系統和 Managed 程式代碼中發生的損毀狀態例外狀況,例如 Windows 中的存取違規,以由 Managed 程式代碼處理。 如果您想要在 .NET Framework 4 或更新版本中編譯應用程式,並維護損毀狀態例外狀況的處理,您可以將 屬性套用 HandleProcessCorruptedStateExceptionsAttribute 至處理損毀狀態例外狀況的方法。

設定程式代碼以分析

使用下列選項來設定程式代碼基底要執行此規則的部分。

您可以只針對此規則、針對它套用的所有規則,或針對套用至此類別的所有規則,或針對它套用的所有規則,設定此選項。 如需詳細資訊,請參閱 程式代碼品質規則組態選項

不允許的例外狀況類型名稱

您可以設定不允許攔截哪些例外狀況類型。 例如,若要指定規則應該使用 NullReferenceException來標幟catch處理程式,請將下列機碼/值組新增至專案中的 .editorconfig 檔案:

dotnet_code_quality.CA1031.disallowed_symbol_names = NullReferenceException

選項值中允許的類型名稱格式(以 |分隔):

  • 僅限類型名稱 (包含名稱的所有符號,不論包含類型或命名空間為何)
  • 符號 文件識別碼格式 的完整名稱,前面加上 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
{
    FileStream? inStream;
    FileStream? outStream;

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

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

public class GenericExceptionsCaughtFixed
{
    FileStream? inStream;
    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 {0}.", 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 {0}.", outFile);
            throw;
        }
    }
}

CA2200:必須重新擲回以保存堆疊詳細資料