Exception Handling in SQL Server
How can we use exception handling within stored procedure ? Give me a example.
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Anil KumarRank #367 · 6.8K reputationPosted Jun 5, 2012, 4:49 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Jun 5, 2012, 4:55 AM EDT
You can use try catch in sql for exception handling.
Example:
BEGIN TRY
SELECT * FROM Table1 WHERE id = 24;
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH;
TRY…CATCH uses the following error functions to capture error information:
- ERROR_NUMBER() : returns the error number.
- ERROR_MESSAGE(): returns the complete text of the error message. The text includes the values supplied for any substitutable parameters such as lengths, object names, or times.
- ERROR_SEVERITY() : returns the error severity.
- ERROR_STATE(): returns the error state number.
- ERROR_LINE(): returns the line number inside the routine that caused the error.
- ERROR_PROCEDURE(): returns the name of the stored procedure or trigger where the error occurred.
please referhttp://www.codeproject.com/Articles/12080/Using-TRY-CATCH-in-Transact-SQL
hope this will help u.