CA2200: Eseguire il rethrow per conservare i dettagli dello stack

Proprietà valore
ID regola CA2200
Title Eseguire il rethrow per mantenere i dettagli dello stack
Categoria Utilizzo
Correzione che causa un'interruzione o un'interruzione Nessuna interruzione
Abilitato per impostazione predefinita in .NET 8 Come avviso

Causa

Un'eccezione viene rigenerata e l'eccezione viene specificata in modo esplicito nell'istruzione throw .

Descrizione regola

Una volta generata un'eccezione, parte delle informazioni che contiene è l'analisi dello stack. L'analisi dello stack è un elenco della gerarchia di chiamate al metodo che inizia con il metodo che genera l'eccezione e termina con il metodo che intercetta l'eccezione. Se viene generata nuovamente un'eccezione specificando l'eccezione nell'istruzione , l'analisi throw dello stack viene riavviata nel metodo corrente e l'elenco di chiamate di metodo tra il metodo originale che ha generato l'eccezione e il metodo corrente viene perso. Per mantenere le informazioni di traccia dello stack originali con l'eccezione, usare l'istruzione throw senza specificare l'eccezione.

Se si sta rigenerando l'eccezione da un punto diverso dal gestore (catch blocco), usare ExceptionDispatchInfo.Capture(Exception) per acquisire l'eccezione nel gestore e ExceptionDispatchInfo.Throw() quando si vuole rigenerarla. Per altre informazioni, vedere Acquisire le eccezioni da rigenerare in un secondo momento.

Come correggere le violazioni

Per correggere una violazione di questa regola, generare nuovamente l'eccezione senza specificare l'eccezione in modo esplicito.

Quando eliminare gli avvisi

Non escludere un avviso da questa regola.

Esempio

Nell'esempio seguente viene illustrato un metodo , CatchAndRethrowExplicitly, che viola la regola e un metodo , CatchAndRethrowImplicitly, che soddisfa la regola.

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

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

        try
        {
            CatchAndRethrowImplicitly();
        }
        catch (ArithmeticException e)
        {
            Console.WriteLine("{0}Implicitly specified:{0}{1}",
               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