Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Stack Trace difference between Debug and Release mode

Tags:

c#

The code below generates different exception stack trace in both debug and release mode:

static class ET
{
    public static void E1()
    {
        throw new Exception("E1");
    }
    public static void E2()
    {
        try
        {
            E1();
        }
        catch (Exception e)
        {

            throw;

        }
    }

    public static void Entry()
    {
        try
        {

            E2();
        }
        catch (Exception e)
        {           
            Console.WriteLine(e.StackTrace);
        }
    }
}

Result in Debug Mode:

at ET.E1() in D:\myStudio\CSharp\CSharp4.0\MyCSharp\ExceptionHandling.cs:line 47

at ET.E2() in D:\myStudio\CSharp\CSharp4.0\MyCSharp\ExceptionHandling.cs:line 58

at ET.Entry() in D:\myStudio\CSharp\CSharp4.0\MyCSharp\ExceptionHandling.cs:line 68

Result in Release Mode:

at ET.E2() in D:\myStudio\CSharp\CSharp4.0\MyCSharp\ExceptionHandling.cs:line 55

at ET.Entry() in D:\myStudio\CSharp\CSharp4.0\MyCSharp\ExceptionHandling.cs:line 68

Please note that the first line from the result in Release mode is missing. How to return the offending line in release mode.

like image 227
Pingpong Avatar asked Sep 28 '11 14:09

Pingpong


1 Answers

You are probably seeing the result of inlining. When you compile in debug mode, inlining is always turned off (so that debugging makes sense). When you compile in release mode, the compiler will remove certain methods (subject to a lot of rules) and insert their content into all of the call sites. This improves the overall performance of those methods by removing the method call overhead.

like image 187
Chris Shain Avatar answered Sep 18 '22 16:09

Chris Shain