3

Possible Duplicate:
incorrect stacktrace by rethrow

I read the question Why catch and rethrow an exception in C#? regarding the differences between

try {
  // do stuff that might throw an exception
} catch (Exception e) {
  throw e; // this destroys the strack trace information!
}

AND

try {
  // do stuff that might throw an exception
} catch (Exception e) {
  throw ; //this preserves the stack trace
}

From it I would assume that the ex.StackTrace in the Main method would point to the same line of code as the ex.StackTrace in the Division method. But this is not happening. I have marked lines 33 and 40 for convenience.

If I change the throw in the Division method to throw ex I see the same output as shown below. The question linked to above discusses the difference be between these two throws but I see no difference in the output.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionTests
{
    class Program
    {
        static void Main(string[] args)
        {
            BadMath b = new BadMath();
            try
            {
                int result =  b.Division(2, 0);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("\n\nException caught in Main Method");
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(ex.StackTrace);
            }
        }
    }

    class BadMath
    {
        public int Division(int a, int b)
        {
            try
            {
    LINE 33      return a / b;
            }
            catch (DivideByZeroException ex)
            {
                System.Console.WriteLine("DivideByZeroException caught in Division Method");
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(ex.StackTrace);
    LINE 40     throw;
            }
        }
    }
}

Output

DivideByZeroException caught in Division Method
Attempted to divide by zero.
   at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 33


Exception caught in Main Method
Attempted to divide by zero.
   at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 40
   at ExceptionTests.Program.Main(String[] args) in c:\testdev\ExceptionTests\Ex
ceptionTests\Program.cs:line 16

Solution

The issue is not apparent in the the above example but is obvious in the following.

namespace ExceptionTests
{
    class Program
    {
        static void Main(string[] args)
        {
            BadMath b = new BadMath();
            try
            {
                int result =  b.Division(2, 0);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("\n\nException caught in Main Method");
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(ex.StackTrace);
            }
        }
    }

    class BadMath
    {
        public int Division(int a, int b)
        {
            try
            {
                return DoDivision(a, b);
            }
            catch (DivideByZeroException ex)
            {
                System.Console.WriteLine("DivideByZeroException caught in Division Method");
                System.Console.WriteLine(ex.Message);
                System.Console.WriteLine(ex.StackTrace);
                throw ;
                //throw ex ;
            }

        }

        public int DoDivision(int a, int b)
        {
            return a / b;
        }
    }
}

output when using throw

Exception caught in Main Method
Attempted to divide by zero.
   at ExceptionTests.BadMath.DoDivision(Int32 a, Int32 b) in c:\testdev\Exceptio
nTests\ExceptionTests\Program.cs:line 47
   at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 40
   at ExceptionTests.Program.Main(String[] args) in c:\testdev\ExceptionTests\Ex
ceptionTests\Program.cs:line 16

output when using throw ex

Exception caught in Main Method
Attempted to divide by zero.
   at ExceptionTests.BadMath.Division(Int32 a, Int32 b) in c:\testdev\ExceptionT
ests\ExceptionTests\Program.cs:line 40
   at ExceptionTests.Program.Main(String[] args) in c:\testdev\ExceptionTests\Ex
ceptionTests\Program.cs:line 16
Community
  • 1
  • 1
tom
  • 1,822
  • 4
  • 25
  • 43
  • @FrédéricHamidi maybe my understanding of what should happen is wrong. I assume that "preserving the stack trace" means that that I see the line number where the original exception was caused? Is that correct? – tom Jan 16 '13 at 23:49
  • 2
    This is almost exactly the same question: http://stackoverflow.com/questions/4217616/incorrect-stacktrace-by-rethrow – itsme86 Jan 16 '13 at 23:49
  • use ex.ToString() and see what happens. Then experiment with `throw ex;` and see how that differs. – John Saunders Jan 16 '13 at 23:53
  • Figured it out. Thanks to all. – tom Jan 17 '13 at 00:08

0 Answers0