Jon*_*ric 7 c# debugging exception-handling visual-studio
重新抛出异常时的通常建议是使用throw;语句,以便保留原始堆栈跟踪.(例子)
但是,当我尝试这个简单的示例时,Visual Studio调试器不显示原始堆栈跟踪.
namespace ExceptionTest
{
class Program
{
static void ThrowException()
{
throw new System.Exception(); // The line that I WANT the debugger to show.
}
static void Main(string[] args)
{
try
{
ThrowException();
}
catch (System.Exception)
{
System.Console.WriteLine("An exception was thrown.");
throw; // The line that the debugger ACTUALLY shows.
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用调试器查找异常的原始来源?
您最好的选择是让Visual Studio打破原始异常,而不是从堆栈跟踪导航回来.去做这个:
1)单击"Debug"菜单项2)单击"Exceptions ..."3)选择"Common Language Runtime Exceptions" - "Thrown"
使用这种方法,如果抛出许多异常,您可能会得到比您真正想要的更多.您可以通过展开树列表来过滤它中断的异常.
见图:
