I am trying to understand Exception handling in C#. I have a sample program.
class Program
{
static void Main(string[] args)
{
Program p = new Program();
try
{
p.Method2();
}
catch (Exception ex)
{
Console.WriteLine("Enter into Main()");
Console.WriteLine("The original Stack Trace Line No 47 is missing in current Stack Trace.");
Console.WriteLine("------------------------------------------------------------------------");
Console.Write(ex.StackTrace.ToString());
Console.ReadKey();
}
}
private void Method2()
{
try
{
Method1();
}
catch (Exception ex)
{
//throw ex resets the stack trace Coming from Method 1 and propogates it to the caller(Main)
throw;
}
}
private void Method1()
{
try
{
throw new Exception("Inside Method1");
}
catch (Exception ex)
{
Console.WriteLine("Exception " + ex);
throw;
}
}
}
I would like to see the original stack trace line number in the main method(which I can see in the catch block of the Method1). Is this possible?