我读了关于
之间差异的问题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!
}
AND
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行。
如果我将Division方法中的throw
更改为throw ex
,我会看到相同的输出,如下所示。与上述相关的问题讨论了这两个throw
之间的差异,但我发现输出没有差异。
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