当捕捉异常 [英] Where to catch exceptions

查看:159
本文介绍了当捕捉异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF SVC分离成一个服务层,业务逻辑层和数据访问层。

I have a WCF svc separated into a Service Layer, Business Logic Layer and Data Access Layer.

在我的DAL遇到异常,我应该抓住它那里还是让它泡泡背到服务层?为什么?

When my DAL encounters an exception, should I catch it there or let it bubble back up to the Service Layer? And why?

请忽略任何客户端参与的这种情况下,我只关心登录异常的WCF SVC。

Please disregard any client involvement for this scenario, I am only concerned with logging the exceptions on the WCF svc.

推荐答案

这也取决于你是如何架构的解决方案。例如,如果DAL和BLL层意味着是完全独立的元件,则它们不能对谁在呼叫他们的假设。因此,它们应该在组件边界在两个捕获异常,登录这些例外,然后允许例外传播。他们可能想在一层特定的异常包裹一般例外:

It depends also on how you are architecting your solution. For instance, if the DAL and BLL layers are meant to be totally independent components, then they cannot make assumptions about who is calling them. As such, they should both catch exceptions on the component boundary, log those exceptions, then allow the exception to propagate. They may want to wrap the general exception in a layer-specific exception:

catch (Exception ex)
{
    Logger.Log(ex);
    throw new DalException("Unhandled exception in DAL", ex);
}

如果您的知道的这些只会被用来作为整个应用程序的一部分,那么你可以推迟记录到的最外层 - 也就是说,如果它不捕获异常层,该异常不会被记录下来。

If you know these will only be used as part of your overall application, then you can defer logging to the outermost layer - the layer that, if it doesn't catch the exception, the exception won't be logged.

这篇关于当捕捉异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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