Monday, October 22, 2012

Error Handling In Store Procedure


                                Error Handling In Store Procedure
The TRY block contains the SQL statements that may raise an error and CATCH block contains the handling mechanism to process the error. When any error is raised in the TRY block the control is immediately transferred to the CATCH block, where the Error is handled.

Following rules should be taken care off while using TRY-CATCH constructs:
- A TRY block must be followed immediately by the CATCH block.
- Both TRY & CATCH blocks must be inside a Batch, Stored Procedure or a Trigger.
- Only Errors with severity between 10 & 20 are caught & handled by TRY-CATCH constructs.
- As per MS BOL, Errors that have a severity of 20 or higher that cause the Database Engine to close the connection will not be handled by the TRY…CATCH block. And Errors that have a severity of 10 or lower are considered warnings or informational messages, and are not handled by TRY…CATCH blocks.

No comments:

Post a Comment