Condividi tramite


Eseguire il rethrow per conservare i dettagli dello stack

Aggiornamento: novembre 2007

TypeName

RethrowToPreserveStackDetails

CheckId

CA2200

Category

Microsoft.Usage

Breaking Change

Non sostanziale

Causa

Viene rigenerata un'eccezione e quest'ultima è specificata in modo esplicito nell'istruzione throw.

Descrizione della regola

Dopo che è stata generata un'eccezione, parte delle informazioni in essa contenute è data dall'analisi dello stack. ‎L'analisi dello stack è un elenco della gerarchia delle chiamate ai metodi che inizia con il metodo che genera l'eccezione e termina con quello che la rileva. Se un'eccezione viene rilanciata specificandola nell'istruzione throw, l'analisi dello stack viene riavviata in corrispondenza del metodo corrente e l'elenco delle chiamate ai metodi tra il metodo originale che ha generato l'eccezione e il metodo corrente va perso. Per mantenere con l'eccezione le informazioni dell'analisi dello stack originale, utilizzare l'istruzione throw senza specificare l'eccezione.

Correzione di violazioni

Per correggere una violazione di questa regola, rigenerare l'eccezione senza specificarla in modo esplicito.

Esclusione di avvisi

Non escludere un avviso da questa regola.

Esempio

Nell'esempio riportato di seguito vengono illustrati il metodo CatchAndRethrowExplicitly che viola la regola e il metodo CatchAndRethrowImplicitly che la soddisfa.

Imports System

Namespace UsageLibrary

   Class TestsRethrow

      Shared Sub Main()
         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
using System;

namespace UsageLibrary
{
   class TestsRethrow
   {
      static void Main()
      {
         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 e)
         {
            // Satisfies the rule.
            throw;
         }
      }

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