Pingpong
Pingpong

Reputation: 8009

Exception Stack Trace difference between Debug and Release mode

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.

Upvotes: 13

Views: 5976

Answers (1)

Chris Shain
Chris Shain

Reputation: 51329

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.

Upvotes: 19

Related Questions