What should be the strategy to handle the sql exceptions?

末鹿安然 提交于 2019-12-13 13:44:35

问题


I have a multi-tier application. DAL -> BAL -> Business Gateway ->UI. If a foreign key or unique constraint exception occurs at DAL, How should we recognize which exception is this and what error message to show the user. Should we use error number to identify this.

Second question : How should we propogate this error to UI. We are thinking to throw this exception to BAL and BAL would encapsulate the error and return a response(not exception) to UI. Is it the right approach.


回答1:


DAL exceptions should be handled and rethrown (as custom exceptions) by your BAL. Which you should serialize and send over to your UI via communication channel that you're using.

You don't need to show exact error to the user or error number because that would form bad user experience. You can just tell them that you could not perform the operation in db. You can log the details in some log file or windows event log.

EDIT: SqlException has error code which you can check in DAL and then throw a specific exception based on that. Your DAL abstracts out SQL Server as backend store and therefore SqlExceptions should not leak outside your DAL. So within DAL check the ErrorCode and throw a specific exception.



来源:https://stackoverflow.com/questions/7782451/what-should-be-the-strategy-to-handle-the-sql-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!