Shantanu Gupta
Shantanu Gupta

Reputation: 21108

What should be the strategy to handle the sql exceptions?

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.

Upvotes: 3

Views: 310

Answers (1)

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35156

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.

Upvotes: 3

Related Questions