Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
| Property | Värde | 
|---|---|
| Regel-ID | CA2200 | 
| Title | Återväxt för att bevara stackinformation | 
| Kategori | Användning | 
| Korrigeringen är icke-bakåtkompatibel | Icke-icke-bryta | 
| Aktiverad som standard i .NET 9 | Som varning | 
Orsak
Ett undantag ändras 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 ändras genom att du anger undantaget i -instruktionen throw startas stackspårningen om med den aktuella metoden och listan över metodanrop mellan den ursprungliga metoden som utlöste 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 återväxar undantaget från någon annan plats än hanteraren (catch blocket) använder ExceptionDispatchInfo.Capture(Exception) du för att avbilda undantaget i hanteraren och ExceptionDispatchInfo.Throw() när du vill återväxa 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 återväxa undantaget utan att uttryckligen ange undantaget.
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