Delen via


CA2200: Opnieuw verzamelen om stackdetails te behouden

Eigenschappen Weergegeven als
Regel-id CA2200
Titel Opnieuw verzamelen om stackdetails te behouden
Categorie Gebruik
Oplossing is brekend of niet-brekend Niet-brekend
Standaard ingeschakeld in .NET 8 Als waarschuwing

Oorzaak

Er wordt een uitzondering opnieuw gestart en de uitzondering wordt expliciet opgegeven in de throw instructie.

Beschrijving van regel

Zodra er een uitzondering wordt gegenereerd, is een deel van de informatie die wordt meegevoerd de stacktracering. De stacktracering is een lijst met de methode-aanroephiƫrarchie die begint met de methode die de uitzondering genereert en eindigt met de methode die de uitzondering onderschept. Als een uitzondering opnieuw wordt gestart door de uitzondering in de throw instructie op te geven, wordt de stack-trace opnieuw gestart bij de huidige methode en wordt de lijst met methodeaanroepen tussen de oorspronkelijke methode die de uitzondering heeft veroorzaakt en de huidige methode verloren gegaan. Als u de oorspronkelijke stack-traceringsgegevens met de uitzondering wilt behouden, gebruikt u de throw instructie zonder de uitzondering op te geven.

Als u de uitzondering vanaf een andere locatie dan de handler (catch blok) wilt beperken, gebruikt ExceptionDispatchInfo.Capture(Exception) u deze om de uitzondering vast te leggen in de handler en ExceptionDispatchInfo.Throw() wanneer u deze wilt verwijderen.

Zie Uitzonderingen vastleggen en opnieuw werpen voor meer informatie.

Schendingen oplossen

Als u een schending van deze regel wilt oplossen, gooit u de uitzondering opnieuw zonder expliciet de uitzondering op te geven.

Wanneer waarschuwingen onderdrukken

Een waarschuwing van deze regel niet onderdrukken.

Opmerking

In het volgende voorbeeld ziet u een methode, CatchAndRethrowExplicitlydie in strijd is met de regel en een methode, CatchAndRethrowImplicitlydie voldoet aan de regel.

class TestsRethrow
{
    static void Main2200()
    {
        TestsRethrow testRethrow = new TestsRethrow();
        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

Zie ook