扔 . .然后...重新投掷 [英] throw . .then ... rethrowing

查看:111
本文介绍了扔 . .然后...重新投掷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用throw ex&只扔&说重新抛出异常...然后为什么在C#中没有重新抛出关键字...?
重新投掷背后的确切理论是什么?


一级较高级别的意思是将异常消息传递到例如数据层到业务层或诸如此类.... ??/

we use throw ex & only throw & say rethrowing an exception...then why not is there a keyword of rethrow in c#...?
what is the exactly theory behind of rethrowing?


one level & higher level mean as e pass exception message to for example data layer to business layer or like this ....??/

推荐答案

如果您的问题是为什么人们捕获异常并将其重新抛出,那么完成此操作的一种非常常见的情况是记录日志.捕获到异常,添加了日志条目,然后重新抛出该异常.调用代码可以抑制该异常,但是日志中仍然会引用错误.

另一种情况是在某些情况下有选择地抛出,而在另一些情况下则无声地吞下它或抛出已包装的异常.

还有其他可能的用法,但是我希望这两个用法对您来说足够清楚.
If your question is why would people catch an exception and re-throw it, then one very common situation where this is done is for logging. The exception is caught, a log entry added, and then rethrown. The calling code may suppress the exception but the log will still have the error referenced.

Another situation is to selectively throw under certain circumstances, and in others to silently swallow it or throw a wrapped exception.

There are other possible uses too but these two should make it clear enough for you I hope.


异常重新抛出(只是不带参数地抛出")非常有用,但这是无语义的.与业务层和其他荒谬无关.它是否会继续在堆栈中继续传播异常 .

基本上,您需要仅在每个线程的顶部捕获所有异常,以防止应用程序终止.在所有其他情况下,您都需要重新抛出,以防止阻止异常传播.在少数情况下,阻止传播(最终"处理例外)通常是作为一些您无法改进的不良"第三方代码的变通方法.

通常,仅在针对特定情况添加某些处理时才需要重新抛出,但不能完全解决问题,因此,此时您仅执行代码可以胜任的处理,并将处理扩展到堆栈上以保留它给更一般的处理程序.如果您不执行此部分处理,则不应捕获它.通常,您需要捕获一种异常并引发另一种异常.

通常的错误不是错误处理异常,而是在没有目的的情况下捕获它.非常典型的完全错误的样式正在捕获某些功能中的所有异常.在单独的函数中包含一些代码是捕获的非常不好的原因.一个规则是:如果您不知道如何处理具体的异常类型,请不要捕获它.但是,您始终只需要捕获每个线程的堆栈顶部即可. UI线程中的捕获异常是单独的,请参见下文.

有关更多详细信息,请参见我关于使用异常的说明:
我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,异常是捕获了如何处理? [
The exception re-throw (just "throw" without argument) of very useful, but it is semantic-free. Nothing about business layer and other absurdity. Is it continues propagation of exception up the stack.

Basically, you need to catch all the exception only on top of each thread, to prevent termination of the application; in all other cases you need to re-throw, just to prevent blocking of exception from propagation. There is a limited number of cases when propagation is blocked (exception is "finally" processed), usually as a work-around of some "bad" third-party code which you cannot improve.

Usually, you only need to re-throw when you add some processing for a specific case but it does not fix the problem completely, so you do only the processing your code is competent about at this point and propagate processing up the stack to leave it to a more general handler. If you don''t do this partial processing, you should not catch it. Very typically, you need to catch one type of exception and throw another one.

Usual mistake is not wrong handling an exception but catching it without the purpose. Very typical completely wrong style is catching all exception in some function. Having some code in a separate function is a very bad reason for catching. A rule is: if you don''t know what to do with a concrete type of exception, don''t catch it. But you always need to catch only on top of the stack of each thread. Catching exception in UI thread is separate, see below.

For further detail, please see my instruction on using exceptions:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^].

—SA


我不太明白您的问题.要抛出try/catch块中捕获的异常,请执行以下操作:

I don''t quite understand your question. To rethrow an exception that is caught in a try/catch block do something like the following:

try
{
    throw new Exception();
}
catch(Exception ex)
{
    throw ex;
}



不需要rethrow关键字,只需throw例外.在生成网站时经常使用此方法,当您抛出异常时要处理它,然后将其传递到链上,以便可以向用户显示.



There is no need for a rethrow keyword, just throw the exception again. I have used this when producing websites as frequently when an exception is thrown you want to handle it where it occurred then pass it on up the chain so it can be displayed to the user.


这篇关于扔 . .然后...重新投掷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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