在 C# 中找不到 throw 与 throw ex 之间的任何区别
Not finding any difference between throw vs throw ex in C#
我试图理解 throw 和 throw ex 之间的区别。为了辅助我的学习,我对这个理论做了一些研究,这让我找到了以下链接:
Is there a difference between "throw" and "throw ex"?
http://www.c-sharpcorner.com/UploadFile/a777ce/difference-between-throw-and-throw-ex-in-C-Sharp/
difference between throw and throw ex in c# .net
总结以上几点不同之处在于:-
throw re-throws the exception that was caught, and preserves the stack
trace. throw ex throws the same exception, but resets the stack trace
to that method.
所以我继续创建了一个演示应用程序,以查看操作上的差异。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Calc c = new Calc();
c.Test();
}
}
class Calc
{
public void Test()
{
try
{
int a = 10;
int b = 10;
int c = 10 / (a - b);
}
catch (Exception ex)
{
throw; //throw statement
}
}
}
}
这给我的输出为:-
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication1.Calc.Test() in C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line 31
at ConsoleApplication1.Program.Main(String[] args) in C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line 14
现在用 throw ex 替换了 throw。
class Calc
{
public void Test()
{
try
{
int a = 10;
int b = 10;
int c = 10 / (a - b);
}
catch (Exception ex)
{
throw ex; // throw ex statement
}
}
}
这给出了输出:-
Unhandled Exception: System.DivideByZeroException: Attempted to divide
by zero. at ConsoleApplication1.Calc.Test() in
C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line
31 at ConsoleApplication1.Program.Main(String[] args) in
C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line
14
如果我看到异常消息,它们都是相同的。
那么区别在哪里呢?
我同意确实有区别,但为什么我看不到呢?我错过了什么?
您没有看到的问题是,您正在处理的可能抛出位置非常接近,以至于在收集堆栈跟踪时无法区分。
试试看:
public void Test()
{
try
{
Deeper();
}
catch (Exception ex)
{
throw; //throw statement
}
}
private static void Deeper()
{
int a = 10;
int b = 10;
int c = 10 / (a - b);
}
throw;
变体将在堆栈跟踪中显示 Deeper
。 throw ex;
只会显示 Test
作为最深层次。
我试图理解 throw 和 throw ex 之间的区别。为了辅助我的学习,我对这个理论做了一些研究,这让我找到了以下链接:
Is there a difference between "throw" and "throw ex"?
http://www.c-sharpcorner.com/UploadFile/a777ce/difference-between-throw-and-throw-ex-in-C-Sharp/
difference between throw and throw ex in c# .net
总结以上几点不同之处在于:-
throw re-throws the exception that was caught, and preserves the stack trace. throw ex throws the same exception, but resets the stack trace to that method.
所以我继续创建了一个演示应用程序,以查看操作上的差异。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Calc c = new Calc();
c.Test();
}
}
class Calc
{
public void Test()
{
try
{
int a = 10;
int b = 10;
int c = 10 / (a - b);
}
catch (Exception ex)
{
throw; //throw statement
}
}
}
}
这给我的输出为:-
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at ConsoleApplication1.Calc.Test() in C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line 31 at ConsoleApplication1.Program.Main(String[] args) in C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line 14
现在用 throw ex 替换了 throw。
class Calc
{
public void Test()
{
try
{
int a = 10;
int b = 10;
int c = 10 / (a - b);
}
catch (Exception ex)
{
throw ex; // throw ex statement
}
}
}
这给出了输出:-
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero. at ConsoleApplication1.Calc.Test() in C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line 31 at ConsoleApplication1.Program.Main(String[] args) in C:\kgn\personal\workspace\ConsoleApplication1\ConsoleApplication1\Program.cs:line 14
如果我看到异常消息,它们都是相同的。
那么区别在哪里呢?
我同意确实有区别,但为什么我看不到呢?我错过了什么?
您没有看到的问题是,您正在处理的可能抛出位置非常接近,以至于在收集堆栈跟踪时无法区分。
试试看:
public void Test()
{
try
{
Deeper();
}
catch (Exception ex)
{
throw; //throw statement
}
}
private static void Deeper()
{
int a = 10;
int b = 10;
int c = 10 / (a - b);
}
throw;
变体将在堆栈跟踪中显示 Deeper
。 throw ex;
只会显示 Test
作为最深层次。