C# 合约实现和异常
我有一个 BO 方法,必须执行两项操作才能履行其合同。
- 将记录插入数据库
- 发送包含记录元数据的电子邮件通知
两者都是通过调用负责完成操作的各个 DAO 方法来处理的。
我不会在 DAO 中捕获异常,而是在 BO 中处理并发布它们。 第一个调用是对 DAO 进行插入,并且可能抛出 NullReferenceException 或其中一个 sql 异常。 电子邮件方法使用 SmtpClient.Send ,它可能会引发大量异常。
是否可以通过属性强制调用电子邮件功能的子方法仅返回一种类型的异常(自定义异常)?
为了说明这一点:
public void AddSupportTicket(SupportTicketDTO ticket)
{
try
{
supportTicketDAO.Insert(ticket);
email.SendNotification(ticket);
}
catch (ExceptionA exc) { ... } // both of the methods can throw exceptions that
catch (ExceptionB exc) { ... } // can overlap. I just care about returning
catch (ExceptionC exc) { ... } // a custom EmailException or DataException
}
我可以将每个子方法调用包装在自己的 try catch 中,并抛出我想要的自定义异常,但这只是另一级别的 try catch,它被 AddSupportTicket 中的另一个 try catch 捕获,然后在 UI 中进行重定向一个聪明的错误,所以这对我来说听起来不太好。
我应该如何有效地抛出正确的自定义异常?
I have a BO method that must do two operations to fulfill its contract.
- insert a record into the db
- send an email notification containing the metadata for the record
Both are handled with calls to individual DAO methods responsible for completing the action.
I do not trap exceptions in my DAO, I handle and publish them in the BO. The first call is to the DAO to insert, and can throw a NullReferenceException or one of the sql exceptions. The email method uses SmtpClient.Send which can throw a slew of exceptions.
Is it possible to force the child method that calls the email functionality to return only one type of exception, a custom one, through an attribute?
To illustrate this:
public void AddSupportTicket(SupportTicketDTO ticket)
{
try
{
supportTicketDAO.Insert(ticket);
email.SendNotification(ticket);
}
catch (ExceptionA exc) { ... } // both of the methods can throw exceptions that
catch (ExceptionB exc) { ... } // can overlap. I just care about returning
catch (ExceptionC exc) { ... } // a custom EmailException or DataException
}
I can wrap each child method call in its own try catch and throw the custom exception I want, but thats just another level of try catch that gets trapped by another try catch in AddSupportTicket, and then one in the UI to redirect to an intelligent error, so that doesn't sound great to me.
How should I efficiently throw the correct custom exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 WCF,您可以指定可以从给定函数抛出哪种异常,因此在您的服务接口中您将有类似的内容
If your using WCF you can specify what kind of exception can be thrown from a given function, so in your service interface you would have something like
不知道属性,但你不能这样做:
Don't know about an attribute but you could you not just do:
C# 不提供类似于 Java 的
throws
关键字的功能(如果您指的是这种功能)。 您可以通过 XML 文档标准记录您的代码抛出的各种异常,但没有办法通过合同强制执行此操作。最好的选择是在应用程序的各个级别提供适当的文档和智能异常处理,就像听起来一样圆滑。
C# does not provide functionality like Java's
throws
keyword, if that's what you're referring to. You can document the sorts of exceptions that your code my throw via the XML documentation standards, but there's no way to contractually force this.Your best bet is proper documentation and intelligent exception handling at all levels of your application, as glib as that sounds.
是的,这很有可能。 查看企业库异常处理块。 有很多关于引发、包装、记录各层之间的异常以及设置策略的精彩内容,以便您可以根据需要在配置中处理它。 与日志记录应用程序块相结合,这是一个强大的组合。
Yes, it's very possible. Check out Enterprise Library exception handling block. Lots of great stuff on raising, wrapping, logging exceptions from and between layers as well as setting up policies so you can handle it in configuration if you want to. Combined with the logging application block, it's a powerful combination.