CA2200:再次引发以保留堆栈详细信息
类型名 |
RethrowToPreserveStackDetails |
CheckId |
CA2200 |
类别 |
Microsoft.Usage |
是否重大更改 |
否 |
原因
异常被再次引发,在 throw 语句中显式指定了该异常。
规则说明
一旦引发异常,则该异常所携带的信息中有一部分为堆栈跟踪。 堆栈跟踪是方法调用层次结构的列表,以引发该异常的方法开始,以捕捉该异常的方法结束。 如果通过在 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");
}
}
}