共用方式為


CA2200:請重新擲回以保存堆疊詳細資料

型別名稱

RethrowToPreserveStackDetails

CheckId

CA2200

分類

Microsoft.Usage

中斷變更

不中斷

原因

例外狀況 (Exception) 遭到重新擲回,而且已在 throw 陳述式 (Statement) 中明確指定此例外狀況。

規則描述

一旦擲回例外狀況後,它所傳遞的部分資訊為堆疊追蹤。此堆疊追蹤為方法呼叫階層的清單,以擲回例外狀況的方法為開頭,而以攔截例外狀況的方法為結尾。如果例外狀況是透過在 throw 陳述式中指定例外狀況而重新擲回,則會在目前方法重新啟動堆疊追蹤,而在擲回例外狀況之原始方法和目前方法之間呼叫的方法清單則會遺失。若要保留含有例外狀況的原始堆疊追蹤資訊,請使用 throw 陳述式而不指定例外狀況。

如何修正違規

若要修正此規則的違規情形,請重新擲回例外狀況而不需明確指定例外狀況。

隱藏警告的時機

請勿隱藏此規則的警告。

範例

下列範例會顯示違反規則的方法 (CatchAndRethrowExplicitly),以及符合規則的方法 (CatchAndRethrowImplicitly)。

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