查找最初使用Visual Studio C#debugger抛出rethrown异常的位置?

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)

如何使用调试器查找异常的原始来源?

Rob*_*yth 9

您最好的选择是让Visual Studio打破原始异常,而不是从堆栈跟踪导航回来.去做这个:

1)单击"Debug"菜单项2)单击"Exceptions ..."3)选择"Common Language Runtime Exceptions" - "Thrown"

使用这种方法,如果抛出许多异常,您可能会得到比您真正想要的更多.您可以通过展开树列表来过滤它中断的异常.

见图:

在此输入图像描述


Jon*_*ric 7

如果您正在运行Visual Studio 2010 Ultimate,请使用IntelliTrace.

它使抛出的所有异常的记录,并允许你"调试时光倒流",看参数,线程和变量在每次罚球的时间.

(摘自Chris Schmich对类似问题的回答.)