“扔”是什么?靠自己做什么? [英] What does "throw;" by itself do?

查看:74
本文介绍了“扔”是什么?靠自己做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

投掷新Exception()与投掷之间的差异

仅仅获得

What would be the point of just having

catch (Exception)
{
    throw;
}

这是做什么的?

推荐答案

throw 关键字本身只是重新引发 catch 上面的语句。如果您要执行一些基本的异常处理(例如补偿操作,如回滚事务),然后将异常重新引发到调用方法中,这将很方便。

By itself, the throw keyword simply re-raises the exception caught by the catch statement above. This is handy if you want to do some rudimentary exception handling (perhaps a compensating action like rolling back a transaction) and then rethrow the exception to the calling method.

此方法与在变量中捕获异常并抛出该实例相比,它具有一个显着的优势:它保留了原始调用堆栈。如果先捕获(Exception ex)然后抛出ex,则调用堆栈将仅从该throw语句开始,并且您将丢失原始错误的方法/行。

This method has one significant advantage over catching the exception in a variable and throwing that instance: It preserves the original call stack. If you catch (Exception ex) and then throw ex, your call stack will only start at that throw statement and you lose the method/line of the original error.

这篇关于“扔”是什么?靠自己做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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