views:

107

answers:

1

Hi,

This is not so much a problem as advice on best practice really. I am writing an ASP.Net MVC application, and in my DAL i am using NHibernate, but what do you do if an exception is thrown in your DAL?

Do you catch the exception in the DAL, log the error and then re-throw the exception? Do you not even attempt to catch exeptions at all and use the Application_Error() method in the global.asax as a generic catch all? Do you catch the exception log it and return a bool to the controller indicating a success or failure, or do you do something completly different?

Leading on from this how then do you handle informing the users? Do you show a generic "Error Occured - please try again" type page or do you show a more informative error?

+1  A: 

This is exactly one of those 'it depends' questions. This is what I do:

  • Handle all exceptions in Application_Error (or similar sink-like location)
  • If the exception is base for business logic - say cannot have duplicates, just catch it and act upon it.
  • If it is an infrastructure exception and there is a good chance you can fix it by retrying - handle it in DAL.
  • Propagating specific exception info to user has hardly any benefit because usually the user cannot do anything about it anyway. So a generic error message usually makes do.
  • All unexpected and selected expected exceptions need to be logged with as much info as possible. It is also advisable that you get email with the exception info.

Now specifically to NHibernate - if NH throws an exception it is advised that you close and discard the currently active ISession and just fail. Because the session might be in an unknown/inconsistent state and trying to resurrect it can do more harm than good.

Obviously depending on scale and type of your app and number of various systems/programmers/etc. involved you really might to handle the logging yourself.

Rashack