在C#.NET抛扔前的区别 [英] difference between throw and throw ex in c# .net

查看:149
本文介绍了在C#.NET抛扔前的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我之间的区别在短暂扔抛出前
我读罚球商店前面的例外,没有得到这一行。结果
我可以简单的用实例得到这个?

Can anyone tell me difference between throw and throw ex in brief? I read that throw stores previous exceptions, not getting this line.
Can i get this in brief with example?

推荐答案

是 - 重新抛出被捕获的异常,并保留堆栈跟踪。 抛出前抛出同样的异常,但是的重置堆栈跟踪的该方法。

Yes - 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.

除非你的希望的重置堆栈跟踪(即从库的内部运作屏蔽公共用户),一般的是更好的选择,因为你可以看到异常起源其中,

Unless you want to reset the stack trace (i.e. to shield public callers from the internal workings of your library), throw is generally the better choice, since you can see where the exception originated.

我还要提到一个直通catch块:

I would also mention that a "pass-through" catch block:

try
{
   // do stuff
}
catch(Exception ex)
{
    throw;
}



是没有意义的。这是完全相同的行为,如果没有的try / catch 可言。

这篇关于在C#.NET抛扔前的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆