Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly handle errors in an n-tier application?

I have struggled with this since day 1. It probably doesn't help that I've been surrounded by a lot of code that doesn't even handle errors at all.

Anyway, I'm working with WebForms in your traditional n-tier design: UI->BLL->DAL. Usually what I do (and I know it's not right) is to try/catch my data operations. If there is an exception I simply throw it to bubble up.

try
'db operations
catch ex as exception
throw
finally
'close connections
end

So then it bubbles up to the BLL and in there is another try/catch where I'll log the error. Now I want to alert the user that something is wrong so I throw it again, this way it bubbles to the UI. At the UI level, I will wrap in a try/catch and if there's an error I'll display a friendly message to them.

What are your thoughts? What can I do better here?

like image 220
Mike Avatar asked Sep 30 '09 11:09

Mike


People also ask

What are the ways to handle errors?

Do: Work on exceptions that directly impact the user experience. Don't: Build KPIs around reducing errors or exceptions by a certain percentage or targeting a specific number. Do: Prioritize errors related to your user's personal identifiable information, billing cycle, and functions that could corrupt your database.

How do you handle exceptions at application level?

In any typical application, Exceptions are generally handled at the code level using try-catch blocks. If the code does not have a try and catch block for the occurred exception then it propagates to the page level, at page level the Page_Error routine can be used to handle the exception.

Which is the best way to handle errors in net?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web. config file.

What are N tier Web applications explain with suitable examples?

N-tier data applications are data applications that are separated into multiple tiers. Also called "distributed applications" and "multitier applications", n-tier applications separate processing into discrete tiers that are distributed between the client and the server.


1 Answers

As I understand, you have three try/catches - one for each tier: DAL, BLL,UI.

You should only catch when you are going to do something about it.

From what I understand, you simply retrow from DAL so there's no need for it.

From BLL you log the exeption and that is a good practice since you'll be able to collect data about exceptions to eventually improve the application.

Then you catch at the UI tier to translate the exception into something user friendly. That's OK.

So I would only get rid of the try/catch from the DAL layer - if you really don't do anything more than retrowning the exception.

In some scenarios, it can be usefull to add an identifier at the BLL that is passed to the UI exception and shown to the end users so that if they call support, support personel can correlate the given Id with an exception on the server's log.

That can be done, for instance, adding a Guid or something else meaningfull and unique to Exception.Data collection.

like image 116
Alfred Myers Avatar answered Oct 25 '22 14:10

Alfred Myers