CA2200 : Levez à nouveau une exception pour conserver les détails de la pile

TypeName

RethrowToPreserveStackDetails

CheckId

CA2200

Catégorie

Microsoft.Usage

Modification avec rupture

Modification sans rupture

Cause

Une exception est à nouveau levée et est spécifiée explicitement dans l'instruction throw.

Description de la règle

Une fois qu'une exception est levée, une partie des informations qu'elle véhicule constitue la trace de la pile. La trace de la pile consiste en une liste de la hiérarchie d'appels de méthode, qui démarre avec la méthode qui lève l'exception et s'achève avec la méthode qui intercepte l'exception. Si une exception est à nouveau levée par sa spécification dans l'instruction throw, la trace de la pile est redémarrée au niveau de la méthode actuelle et la liste des appels de méthode présents entre la méthode d'origine qui a levé l'exception et la méthode actuelle est perdue. Pour conserver les informations de traçage de la pile d'origine avec l'exception, utilisez l'instruction throw sans spécifier l'exception.

Comment corriger les violations

Pour résoudre une violation de cette règle, levez à nouveau l'exception sans la spécifier explicitement.

Quand supprimer les avertissements

Ne supprimez aucun avertissement de cette règle.

Exemple

L'exemple suivant présente une méthode, CatchAndRethrowExplicitly, qui viole la règle et une méthode, CatchAndRethrowImplicitly, qui satisfait à la règle.

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