CA2200: Relançar para preservar os detalhes de pilha

TypeName

RethrowToPreserveStackDetails

CheckId

CA2200

<strong>Categoria</strong>

Microsoft.Usage

Alteração significativa

Não separável

Causa

Uma exceção relançada e a exceção for especificada explicitamente na throw instrução.

Descrição da regra

Depois que uma exceção é lançada, a parte das informações que ele executa é o rastreamento de pilha. O rastreamento de pilha é uma lista da hierarquia de chamada de método que inicia com o método que lança a exceção e termina com o método que captura a exceção. Se uma exceção é re-thrown, especificando a exceção a throw o rastreamento de pilha é reiniciado no método atual de instrução, e a lista de chamadas de método entre o método original que lançou a exceção e o método atual é perdida. Para manter as informações de rastreamento de pilha original com a exceção, use o throw a instrução sem especificação de exceção.

Como corrigir violações

Para corrigir uma violação desta regra, relançar a exceção sem especificar explicitamente a exceção.

Quando suprimir avisos

Não suprimir um aviso da regra.

Exemplo

O exemplo a seguir mostra um método, CatchAndRethrowExplicitly, que viola a regra e um método CatchAndRethrowImplicitly, que satisfaz a regra.

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");
      }
   }
}