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

屬性
規則識別碼 CA2200
職稱 重新擲回以保存堆疊詳細資料
類別 使用方式
修正是造成中斷還是不中斷 不中斷
在 .NET 10 中預設啟用 作為警告
適用語言 C# 與 Visual Basic

原因

重新擲回例外狀況,並在 throw 語句中明確指定該例外狀況。

規則描述

一旦擲回例外狀況,其中部分資訊為堆疊追蹤。 堆疊追蹤是方法呼叫階層的清單,從擲回例外狀況的方法開始,並以攔截例外狀況的方法作結。 如果在throw語句中指定例外來重新擲回它,堆疊追蹤會在目前的方法上重新啟動,且擲出例外的原始方法與目前方法之間的方法呼叫清單會遺失。 若要保留有例外狀況的原始堆疊追蹤資訊,請用 throw 陳述式而不要指定例外狀況。

如果您要從處理常式 (catch 區塊) 以外的某個位置重新擲回例外狀況,請使用 ExceptionDispatchInfo.Capture(Exception) 來擷取處理常式中的例外狀況,以及 ExceptionDispatchInfo.Throw() (當您想要重新擲回時)。

如需詳細資訊,請參閱 正確捕捉和重新擲回例外

如何修正違規

若要修正此規則的違規,請重新擲回例外狀況,而不需明確指出例外狀況。

隱藏警告的時機

請勿隱藏此規則的警告。

範例

下列範例顯示違反規則的方法 CatchAndRethrowExplicitly,以及滿足規則的方法 CatchAndRethrowImplicitly

class TestsRethrow
{
    static void Main2200()
    {
        TestsRethrow testRethrow = new();
        testRethrow.CatchException();
    }

    void CatchException()
    {
        try
        {
            CatchAndRethrowExplicitly();
        }
        catch (ArithmeticException e)
        {
            Console.WriteLine($"Explicitly specified:{Environment.NewLine}{e.StackTrace}");
        }

        try
        {
            CatchAndRethrowImplicitly();
        }
        catch (ArithmeticException e)
        {
            Console.WriteLine($"{Environment.NewLine}Implicitly specified:{Environment.NewLine}{e.StackTrace}");
        }
    }

    void CatchAndRethrowExplicitly()
    {
        try
        {
            ThrowException();
        }
        catch (ArithmeticException e)
        {
            // Violates the rule.
            throw e;
        }
    }

    void CatchAndRethrowImplicitly()
    {
        try
        {
            ThrowException();
        }
        catch (ArithmeticException)
        {
            // Satisfies the rule.
            throw;
        }
    }

    void ThrowException()
    {
        throw new ArithmeticException("illegal expression");
    }
}
Imports System

Namespace ca2200

    Class TestsRethrow

        Shared Sub Main2200()
            Dim testRethrow As New TestsRethrow()
            testRethrow.CatchException()
        End Sub

        Sub CatchException()

            Try
                CatchAndRethrowExplicitly()
            Catch e As ArithmeticException
                Console.WriteLine("Explicitly specified:{0}{1}",
               Environment.NewLine, e.StackTrace)
            End Try

            Try
                CatchAndRethrowImplicitly()
            Catch e As ArithmeticException
                Console.WriteLine("{0}Implicitly specified:{0}{1}",
               Environment.NewLine, e.StackTrace)
            End Try

        End Sub

        Sub CatchAndRethrowExplicitly()

            Try
                ThrowException()
            Catch e As ArithmeticException

                ' Violates the rule.
                Throw e
            End Try

        End Sub

        Sub CatchAndRethrowImplicitly()

            Try
                ThrowException()
            Catch e As ArithmeticException

                ' Satisfies the rule.
                Throw
            End Try

        End Sub

        Sub ThrowException()
            Throw New ArithmeticException("illegal expression")
        End Sub

    End Class

End Namespace

另請參閱