The usual advice when rethrowing an exception is to use a throw; statement so the original stack trace is preserved. (Example)
However, when I try this simple example, the Visual Studio debugger does not show the original stack trace.
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.
}
}
}
}
How can I use the debugger to find the original source of the exception?
If you’re running Visual Studio 2010 Ultimate, use IntelliTrace.
It keeps a record of all exceptions thrown and allows you to “debug back in time” to see parameters, threads, and variables at the time of each throw.
(Taken from Chris Schmich’s answer to a similar question.)