Home  /  Questions  /  Question



250   96.9
Nov 14, 2015


Exception Handling in multi-tier applications

Hello,

In a 3-tier system where should we put the error handler ?  One in each layer?  Or just on the service layer (where it handles error of validation BL and DAL) and the presentation layer (as information to the user side) ?

Best Regards,
Eric Santoso



508   99.9
Nov 17, 2015
Eric: 

If the purpose of catching exceptions is to collect execution context information and then re-throw a new Exception, then that should be fine. Generally, well designed web applications have very few places that actually handle exceptions. 

Jack
 1 comment
 
hello, I still use windows apps, Thanks for the response best regards, Eric Santoso --- Eric Santoso  Nov 20, 2015

250   96.9
Nov 17, 2015
this time I have done is, to create a custom exception to DAL and SL ...

if something goes wrong in the DAL,
I will write a log and then re-throw with new custom exception (for DAL)

in the Service Layer I catch a custom exception (for DAL) and I re-throw again with a custom exception (for SL)
so also happens if the validation process on BL, if the validation does not fit then I will e-throw again with a custom exception (for SL)

at the presentation layer in Model I catch a custom exception (for SL) and I re-throw Application Exception
in the form (windows form) that I catch the exception (global exception)

with this way, did I make a mistake? or I get into bad practice

Best Regards,
Eric Santoso

508   99.9
Nov 16, 2015
You should only catch exceptions when you can actually correct the situation.
If not, there is no point in catching them (other than logging perhaps and then re-throwing them). 

In our own work we mostly rely on the catchall error handler in global asax (you see an example in our .NET Pattern Framework code). That is where we log the exception (to the database) and let the application display the standard error page. 

Hope this helps
Jack