使用 DAL BLL 和 Web 表示层时我应该捕获哪些层的异常
我编写了一个三层 Web 应用程序。 DAL BLL和Web表示层,每一层都有方法,所以问题是我应该在哪里捕获异常(使用try catch),在web?BLL?还是DAL?为什么?谢谢。
I write a three tiers web application. DAL BLL and web Presentation Layer,every tier has methods , so the question is where should I catch exception( using try catch),in web?BLL?or DAL? and why? thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
异常是任何方法接口的一部分。
异常可以是显式的(如 Java 的受检查异常),也可以是隐式的。尽管如此,调用者必须预见异常并决定做什么 - 有时一个方法可能决定只允许异常传播,您调用的事物的异常成为正式接口的一部分。
我的做法:
因此,在 Web/表示层中,我们捕获所有异常并向用户显示友好的消息。往往有少数常见的场景:
我发现UI有时会显示错误信息在这些情况下有所不同,因此我喜欢区分它们。这导致我设计我的业务逻辑接口来抛出相应的异常:TransientException、AuthorizationException、InvalidRequestException。
InvalidRequestException 往往具有有用的负载来帮助 UI 格式化有用的响应。
TransientException 可以包含技术信息(例如 DB XXX 有问题 YYY),不是为了向用户显示,而是作为分布式系统中诊断的辅助。
业务逻辑层负责捕获来自下层的无数问题并为 UI 生成异常。
数据访问层应遵循首次失败数据捕获的原则:捕获问题详细记录错误并抛出一些合适的异常。
An Exception is part of the interface of any method.
the exceptions may be explicit, as in Java's Checked Exceptions, or implicit. None the less the caller must anticipate exceptions and decide what to do - sometimes a method may decide just to allow the exception to propogate, exceptions from things you call become part of your formal interface.
My approach:
Hence, in Web/Presentation tier, we catch all exceptions and display friendly messages to the user. There tend to be a small number of common scenarios:
I find that the UI sometimes displays the error information differently in those cases, and hence I like to distinguish them. This leads me to design my Business Logic interfaces to throw corresponding Exceptions: TransientException, AuthorisationException, InvalidRequestException.
The InvalidRequestException tends to have useful payload to help the UI format a useful response.
The TransientException can include technical information (like DB XXX has problenm YYY) not for display to the user, but as an aid to diagnosis in a distributed system.
The business logic layer is responsible for catching the myriad problems from lower layers and producing the exceptions for the UI.
The Data access layer should follow the principle of First Failure Data Capture: catch problems log the error in detail and throw some suitable exception.
最好在数据库层捕获异常并将其抛出到业务层,而不是从业务层抛出到表示层。
我使用 throw 关键字重新抛出异常,并且不向用户显示异常,只是使用 LOG4NET 或在文件中显示错误消息和日志异常,您会发现这种方式对您来说很容易。
您可以查看这篇文章如何在不隐藏详细信息的情况下抛出异常:仔细处理异常
Its better to catch exception at database layer and throw it to business layer and than from throw from business layer to presentation layer.
I rethrow exception by using throw keyword and do not display exception to user just show the error message and log exception using LOG4NET or in the file which way you find easy for you.
You can check this post how to throw exception without hiding the detail : Handle Exception Carefully
除非您确定要处理错误,否则不要在所有方法中使用 try catch。您可能可以在 DAL 中使用 catch ,记录异常并将其抛出到该层。
请参阅这篇文章异常处理
Dont use try catch in all your methods unless you are sure to handle the error.You can probably use catch in DAL ,log the exception and throw it up the layer.
see this post Exception handling
Pranay Rana 给出的链接描述了从层传播异常的正确方法,但是,没有必要在底层(DAL、BAL)中的 try catch 中扭曲方法,除非您需要对异常采取一些操作。
当退出方法之前必须执行某些代码段时,将方法包装在 try catch 中,例如在退出之前清理 SQL 连接/资源。扭曲你的方法,例如
确保将调用(在 UI 或任何其他层中)包装在 try catch 中。即使方法没有包装在底层的 try catch 中,异常也会在调用点被捕获。
The link given by Pranay Rana, describes the correct way to propagate the exceptions from layers however, it is not necessary to warp methods in try catch in underlying layers( DAL, BAL) until unless you need to take some action against exception.
Wrap the methods in try catch when some code segment is mandatory to be executed before exiting the method e.g cleaning up SQL connection/resources before exit. Warp your methods like
Make sure to wrap the calls (in UI or any other layer) in try catch. The exceptions will be caught at the calling point, even there is the methods are not wrapped in try catch in underlying layers.