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.
I write a three tiers web application. DAL BLL and web Presentation Layer,every tier
Share
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.