Exception stack is corrupt when catching and storing the exception

  • Thread starter Morten Herman Langkjaer
  • Start date
M

Morten Herman Langkjaer

i have som code that does e number of operatoins, if any exception occurs
during the execution path, the exception is stored in a list, and in another
place, the errors are logged. for example, method a calls b, that calls c. c
throws an exception, that b stores. When using the exception.ToString(), only
method b and c appears in the stack trace. Sine the method is called from
several locations, i cannot find the execution paths without attaching a
debugger. Is this a feature/bug in the framework. Can anyone help me get the
full stack trace.

i have created a simple application that proves the problem, it is written
in C#, and is a console application. try reviewing the following:
using System;
using System.Diagnostics;

namespace testStack
{
class Program
{
static void Main(string[] args)
{
try
{
StoredErrorThrower();
}
catch( Exception e )
{
Console.Out.WriteLine();
Console.Out.WriteLine("Non stored exception stack trace");
Console.Out.WriteLine();
Console.Out.WriteLine(e);
}

Console.Out.WriteLine();
Console.Out.WriteLine();
Console.Out.WriteLine("Stored Exception stack trace:");
Console.Out.WriteLine();
Console.Out.WriteLine(error);

Console.Out.WriteLine();
Console.Out.WriteLine();
Console.Out.WriteLine("Stack frame from the stored exception handler");
Console.Out.WriteLine(frame);

}

private static void StoredErrorThrower()
{
try
{
throw new Exception("error");
}
catch( Exception e )
{
frame = new StackTrace();
error = e;
}
throw new Exception("error");
}
private static Exception error;
private static StackTrace frame;
}
}
 
D

dlm@bypsoft

Hi Morten,

have you tried to get full stack, debugging your code with SOS extensions enabled? With stack related commands (!clrstack, !dumpstack, !clrstack -p, !clrstack -l) you should get full picture of what's going on.

Best regards,
dlm@bypsoft
Compare SQL Server, MySQL and Oracle for free - DBTyP.NET
www.bypsoft.com
 
M

Morten Herman Langkjaer

The issue occurs in some production code, but i cant seem to reproduce the
exact error in my debug envorinment. Therefore i want to collect the errors
and then log them afterwards. But i can't seem to have full stack trace
logged, unless i create a new instance of the StackTrace class, bu sincte the
exceptions are a part of the public interface, i cannot just introduce the
combination of StackTrace and Exception without changing the interface.
Therefore i would like to know if there is any way to change the way .NET
calculate the stackTrace to enable full stack logging always.

dlm@bypsoft said:
Hi Morten,

have you tried to get full stack, debugging your code with SOS extensions enabled? With stack related commands (!clrstack, !dumpstack, !clrstack -p, !clrstack -l) you should get full picture of what's going on.

Best regards,
dlm@bypsoft
Compare SQL Server, MySQL and Oracle for free - DBTyP.NET
www.bypsoft.com


Morten Herman Langkjaer said:
i have som code that does e number of operatoins, if any exception occurs
during the execution path, the exception is stored in a list, and in another
place, the errors are logged. for example, method a calls b, that calls c. c
throws an exception, that b stores. When using the exception.ToString(), only
method b and c appears in the stack trace. Sine the method is called from
several locations, i cannot find the execution paths without attaching a
debugger. Is this a feature/bug in the framework. Can anyone help me get the
full stack trace.

i have created a simple application that proves the problem, it is written
in C#, and is a console application. try reviewing the following:
using System;
using System.Diagnostics;

namespace testStack
{
class Program
{
static void Main(string[] args)
{
try
{
StoredErrorThrower();
}
catch( Exception e )
{
Console.Out.WriteLine();
Console.Out.WriteLine("Non stored exception stack trace");
Console.Out.WriteLine();
Console.Out.WriteLine(e);
}

Console.Out.WriteLine();
Console.Out.WriteLine();
Console.Out.WriteLine("Stored Exception stack trace:");
Console.Out.WriteLine();
Console.Out.WriteLine(error);

Console.Out.WriteLine();
Console.Out.WriteLine();
Console.Out.WriteLine("Stack frame from the stored exception handler");
Console.Out.WriteLine(frame);

}

private static void StoredErrorThrower()
{
try
{
throw new Exception("error");
}
catch( Exception e )
{
frame = new StackTrace();
error = e;
}
throw new Exception("error");
}
private static Exception error;
private static StackTrace frame;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top