3

可能重复:
重新抛出错误的堆栈跟踪

我读了这个问题Why catch and rethrow an exception in C#? 关于之间的差异

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

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

由此我假设 Main 方法中的 ex.StackTrace 将指向与 Division 方法中的 ex.StackTrace 相同的代码行。但这并没有发生。为方便起见,我标记了第 33 行和第 40 行。

如果我throw将 Division 方法中的更改为throw ex我会看到相同的输出,如下所示。上面链接的问题讨论了这两个throws 之间的区别,但我认为输出没有区别。

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;
            }
        }
    }
}

输出

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

解决方案

这个问题在上面的例子中并不明显,但在下面的例子中很明显。

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
4

0 回答 0