Exception Handling in SQL Server

Introduction

Handling errors in SQL Server became easy with the number of different ways. SQL Server 2005 has introduced the new option that helps to handle the errors effectively. Sometimes we cannot capture the errors which occurred in the end user. Even if we want to know the errors which occurred in the end user, we need to write the code to send it to us. It creates an additional overhead for the server.

SQL Server 2005 introduced TRY...CATCH statement which helps us to handle the errors effectively in the back end. This handling of the exception can provide additional information about the errors.

TRY...CATCH

The TRY...CATCH statement works the same as in the programming languages. First it gets executed in the SQL statement which we have written in the TRY block and if any error occurs, then it will get executed the CATCH block.

Steps

Step 2: Write the common Stored procedures for handling the current exception in the name of Proc_InsertErrorDetails which will help us to insert the error details into step1 created table.

Step 3: Write a sample procedure and execute with the exception handling using TRY...CATCH statement. Whenever an error occurs, it will call the Proc_InsertErrorDetails and that will insert the error details.

Step 4: Now, check the ErrorTracer table that will list out the captured error details.

Conclusion

This customized error handling techniques help us to improve the back end error details effectively. We have given the date and username details in the table that will help you to find the error details based on the date and username respectively.

thanks for the article;
I think it's simpler that having one in code. but I have a question.

At my current work we have exception handler written in c# and it also logs to a database. Do you think it's possible to use the two at the same time, one to handle exceptions in code and the other in the database?
Is it not going to write two times into the database if a database error occurs?