Exception Handling in SQL Server TRY CATCH

Exception Handling in SQL Server TRY CATCH:



In this post  I will explain “how to handle exceptions in SQL Server” or exception handling in SQL Server for stored procedure by using try catch or error handling in SQL Server.

Sql Server Related Other Post :




Exception Handling in SQL:


To handle exceptions in SQL Server we can use TRY and CATCH blocks.
 Example of use TRY And CATCH blocks in stored procedure we write as like

Syntax of TRY and CATCH block in store procedure:

BEGIN TRY
                            --Write Your Code-----------------------------
END TRY

BEGIN CATCH
                           --Write Code to handle errors----------------
END CATCH



TRY BLOCK : In TRY block we will write our queries. In our SQL statements if any error occurs automatically it will move to CATCH block in that we can handle error messages.

CATCH BLOCK: In CATCH block we will write code to handle exceptions. To handle error messages we have defined Error Functions in CATCH block those are

Here we also Explain some errors:

ERROR_LINE() - This function will return error line number of SQL query which cause to raise error.

ERROR_NUMBER() - This function will return error number which is unique and assigned to it.

ERROR_SEVERITY() - This function will return severity of error which indicates how serious the error is. The values are between 1 and 25.

ERROR_STATE() - This function will return state number of error message which cause to raise error.

ERROR_PROCEDURE() - This function will return name of the procedure where an error occurred.


ERROR_MESSAGE() - This function will return the complete text of the error message which cause to raise error.



Asp.net Related Other Post :




Comments