为什么要重新抛出异常? [英] Why rethrow an exception?

查看:60
本文介绍了为什么要重新抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


为什么要重新抛出异常?难道没有打败

的目的吗?


最好,

Andre

解决方案

有时你可能想要在
之前做一些跟踪逻辑,或者其他什么,允许下一层处理异常。也许我的数据

层想要写入日志,然后重新抛出到业务逻辑层

捕获它并再次尝试操作,或者只是允许它

传播到显示错误的UI层。


尝试

{

DoSomethingDangerous();

}

catch(SillyException ex)

{

WriteExceptionToLog();

扔; //这将让我的UI处理它认为合适的错误。

}


-Tracing,有时它''很好地标记代码中的实际位置

异常来自。通过重新抛出异常,另外一行

诊断信息被添加到异常的堆栈跟踪中...


- 制作一个更多用户友好错误信息。如果一个原始的SQL Server

异常冒出了GUI,那么用户可能就不会理解它了。

为了做一个更友好的消息,我会创建一个新的异常类,

将ApplicationException作为基础,然后传递我的友好字样。消息和基类构造函数的
原始异常......如果你需要将友好消息国际化,你也可以这样做。你可以传递一个字符串

资源作为你的信息而不是英文原版错误

留言...


- 另外,假设我想在我的异常中添加其他信息,例如

SQL查询的实际文本,或者可能是用户身份,或者可能是

日期时间例外。我可以通过创建一个新的异常类来实现这一点,

再次从ApplicationException降序,使新类

序列化,然后添加其他字段&我需要的属性。


" ah **** @ gmail.com"写道:

大家好,

为什么要重新抛出异常?这不是打败
目的吗?

最好的,



我们使用catch-throw进行多项操作,即(如上所述)获取

更多信息,以便我们进行故障排除(作为开发人员)。我们

捕获一个错误,并将其进一步抛出,添加信息为
,然后允许我们的顶级ErrorHandler捕获它并写入

将异常信息发送到日志文件,同时向用户显示

简单消息。


如果设置了EmailOnError位,编程会收到通知

错误发生,时间,日期,简短说明。那时我们

可以检查error.log文件,看看究竟发生了什么。


简单地说,它让用户和开发人员都更容易以某种方式执行某些事情。


另一个原因是如果您正在编写自己的基本库。

如果添加对他们来说是一个异常类型,并且只抛出那些类型的

更高的程序员将能够用

恩典来处理你的异常。


例如:

1.我开发库Shared.dll

2.共享包含:Shared.BaseValue,Shared.CalculateInterest(),

Shared.GeneratePaymentSchedule()

3.共享也可能包含:Shared.CalculateInterestException和

Shared.GeneratePaymentScheduleException

4.程序员谁将使用我的库可以将

CalculateInteres方法调用放入try / catch块并检查

特定异常。


Hi all,

Why would one want to rethrow an exception? Doesn''t that defeat the
purpose?

Best,
Andre

解决方案

Sometimes you might want to do some tracing logic, or whatever, before
allowing the exception to be handled by the next tier. Maybe my data
layer wants to write to a log, then rethrow to the business logic layer
which catches it and tries the operation again, or simply allows it to
propagate to the UI layer which displays an error.

try
{
DoSomethingDangerous();
}
catch (SillyException ex)
{
WriteExceptionToLog();
throw; // This will let my UI handle the error as it sees fit.
}


-Tracing, sometimes it''s good to mark the actual place in code where the
exception came from. By re-throwing the exception the an additional line of
diagnostic information is added to the exception''s stack trace...

- To make a more "user friendly" error message. If a raw SQL Server
exception bubbled up tot he GUI the user would probably not understand it.
To make a more friendly message I would create a new exception class, with
ApplicationException as a base, and then pass my "friendly" message and the
raw exception to the base class constructor... You could also do this if you
need to internationalize your friendly message; you can pass a string
resource as your message rather than the, probably english, original error
message...

- Also, suppose I want to add additional information to my exception such as
the actual text of a SQL query, or perhaps the user identity, or maybe the
datetime of the exception. I can do this by making a new exception class,
again descending from ApplicationException, making the new class
serializeable, and then adding the additional fields & properties that I need.

"ah****@gmail.com" wrote:

Hi all,

Why would one want to rethrow an exception? Doesn''t that defeat the
purpose?

Best,
Andre



We use catch-throw for several things, namely (as mentioned) obtaining
more information for troubleshooting on our end (as developers). We
capture an error, and throw it up further, adding information as
needed, then allowing our top level ErrorHandler to catch it and write
out the exception information to a logfile, all while displaying a
simple message to the user.

Ifthe EmailOnError bit is set, programming receives a notification of
the errors occurrance, time, date, briefe description. At that point we
can check the error.log file for what exactly happened.

Simply put, it makes it easier for both the user and the developer to
perform things certain ways.

Another reason would be if you were writing your own basic libraries.
If you add an exception type to them and only throw those types the
higher up programmers will be able to handle your exceptions with
grace.

For instance:
1. I develop library Shared.dll
2. Shared contains: Shared.BaseValue, Shared.CalculateInterest(),
Shared.GeneratePaymentSchedule()
3. Shared may also then contain: Shared.CalculateInterestException and
Shared.GeneratePaymentScheduleException
4. The programmers who would use my library could put the
CalculateInteres method call into a try/catch block and check for that
specific exception.


这篇关于为什么要重新抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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