Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Usage Warnings
 Rethrow to preserve stack details
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Rethrow to preserve stack details

TypeName

RethrowToPreserveStackDetails

CheckId

CA2200

Category

Microsoft.Usage

Breaking Change

NonBreaking

An exception is re-thrown and the exception is explicitly specified in the throw statement.

Once an exception is thrown, part of the information it carries is the stack trace. The stack trace is a list of the method call hierarchy that starts with the method that throws the exception and ends with the method that catches the exception. If an exception is re-thrown by specifying the exception in the throw statement, the stack trace is restarted at the current method and the list of method calls between the original method that threw the exception and the current method is lost. To keep the original stack trace information with the exception, use the throw statement without specifying the exception.

To fix a violation of this rule, re-throw the exception without specifying the exception explicitly.

Do not exclude a warning from this rule.

The following example shows a method, CatchAndRethrowExplicitly, which violates the rule and a method, CatchAndRethrowImplicitly, which satisfies the rule.

Visual Basic
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
C#
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");
      }
   }
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker