views:

147

answers:

3

I inherited an application with a lot of stored procedures, and many of them have exception handling code that inserts a row in an error table and sends a DBMail. We have ELMAH on the ASP.NET side, so I'm wondering if exception management in the stored procs is necessary. But before I rip it out, I want to ensure that I'm not making a grave mistake because of ignorance about a best practice.

Only one application uses the stored procedures.

When would one prefer using exception management in a SQL Server 2005 stored procedure over handling the exception on the ASP.NET side?

+1  A: 

If there are other applications utilizing these stored procedures then it might make sense to retain the error handling in the stored procedures. In your edit you indicate that this is not the case so removing the exception handling is probably not a bad idea.

In the MSDN article Exception Handling it is outlined when to catch exceptions and when to let them bubble up the stack. It can be argued that it makes sense to handle and log database exceptions that are recoverable from in the stored procedure.

ahsteele
One of the side effects of handling the exception in the stored procedure is that the application can't fail gracefully, and it's harder to figure out what's going on through debugging.
Robert S.
+1  A: 

There is a principle sometimes referred to as "First Failure Data Capture" - ie. it's the responsibility of the first "chunk of code" that indentifies an error to immediately capture it for future diagnosis. In multi-tier architectures this leads to some interesting questions about who "first" actually is.

I believe that it's quite reasonable for the stored procedure to log something to a db (sending an email sounds somewhet overkill for all but the most critical of errors but that's another issue). It cannot assume taht higher layers will be well behaved, you may only have one client now, but you can't predict the future.

The stored procedure can still throw an exception as well as logging. And sometimes in difficult situations being able to correlate errors in the different layers is actually very handy.

I would take a lot of persuading to remove that error logging.

djna
Right logging that an error occurred in the db is not a bad idea but that's different from handling the exception.
ahsteele
A: 

I believe that logging to a table only works for simpler systems where everything is done within a single stored procedure call.

Once the system is complex enough that you implement transactions across database calls, logging to the database within the stored procedure becomes much more of a problem.

The rollbacks undo the logging to the table.

Logic that allows rollbacks and logging, in my opinion, creates too much potential for defects.

Darryl Peterson