Dela via


CA2200: Återkasta för att bevara stackinformation

Egenskap Värde
Regel-ID CA2200
Title Återkastning för att bevara stackinformation
Kategori Användning
Korrigeringen är antingen invasiv eller icke-invasiv Oumbrytbar
Aktiverad som standard i .NET 10 Som varning
Tillämpliga språk C# och Visual Basic

Orsak

Ett undantag kastas om igen och undantaget anges uttryckligen i instruktionen throw.

Regelbeskrivning

När ett undantag har genererats är en del av den information som det bär stackspårningen. Stackspårningen är en lista över metodanropshierarkin som börjar med metoden som genererar undantaget och slutar med metoden som fångar undantaget. Om ett undantag kastas om genom att specificera undantaget i instruktionen throw, startas stackspårningen om med den aktuella metoden och listan över metodanrop mellan den ursprungliga metoden som kastade undantaget och den aktuella metoden går förlorad. Om du vill behålla den ursprungliga stackspårningsinformationen med undantaget använder du -instruktionen throw utan att ange undantaget.

Om du kastar om undantaget från någon annan plats än hanteraren (catch-blocket) använder du ExceptionDispatchInfo.Capture(Exception) för att fånga undantaget i hanteraren och ExceptionDispatchInfo.Throw() när du vill kasta om det.

Mer information finns i Avbilda och återväxa undantag korrekt.

Så här åtgärdar du överträdelser

Åtgärda en överträdelse av den här regeln genom att återkasta undantaget utan att uttryckligen ange det.

När du ska ignorera varningar

Ignorera inte en varning från den här regeln.

Exempel

I följande exempel visas en metod, CatchAndRethrowExplicitly, som bryter mot regeln och en metod, CatchAndRethrowImplicitly, som uppfyller regeln.

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

Se även