TRY CATCH – Exception Handling
Similar to C++, Java and other languages SQL Server also has a mechanism to handle exceptions by using TRY-CATCH construct. 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.
USE [tempdb] GO --// Create a test Stored Procedure CREATE PROC testPrc (@val VARCHAR(10)) AS BEGIN SELECT 1/@val AS operation END GO --// Test for Divide by 0 (Divide by zero error encountered.) BEGIN TRY EXEC testPrc '0' END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ERROR_ID, ERROR_MESSAGE() AS ERROR_MSG, ERROR_SEVERITY() AS ERROR_SEVERITY, ERROR_STATE() AS ERROR_STATE, ERROR_PROCEDURE() AS ERROR_PROCEDURE, ERROR_LINE() AS ERROR_LINE END CATCH GO --// Test for Datatype conversion (Conversion failed when converting the varchar value 'a' to data type int.) BEGIN TRY EXEC testPrc 'a' END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ERROR_ID, ERROR_MESSAGE() AS ERROR_MSG, ERROR_SEVERITY() AS ERROR_SEVERITY, ERROR_STATE() AS ERROR_STATE, ERROR_PROCEDURE() AS ERROR_PROCEDURE, ERROR_LINE() AS ERROR_LINE END CATCH GO --// Test nested TRY-CATCH for "Divide by 0" & "Datatype conversion" errors both. BEGIN TRY EXEC testPrc 'a' END TRY BEGIN CATCH SELECT 'outer block', ERROR_NUMBER() AS ERROR_ID, ERROR_MESSAGE() AS ERROR_MSG, ERROR_SEVERITY() AS ERROR_SEVERITY, ERROR_STATE() AS ERROR_STATE, ERROR_PROCEDURE() AS ERROR_PROCEDURE, ERROR_LINE() AS ERROR_LINE BEGIN TRY SELECT 1/0 AS operation END TRY BEGIN CATCH SELECT 'inner block', ERROR_NUMBER() AS ERROR_ID, ERROR_MESSAGE() AS ERROR_MSG, ERROR_SEVERITY() AS ERROR_SEVERITY, ERROR_STATE() AS ERROR_STATE, ERROR_PROCEDURE() AS ERROR_PROCEDURE, ERROR_LINE() AS ERROR_LINE END CATCH END CATCH GO --// Test for violation of PK Constraint (Violation of PRIMARY KEY constraint 'PK__testTable__2C3393D0'. Cannot insert duplicate key in object 'dbo.testTable'.) BEGIN TRY CREATE TABLE testTable (a INT PRIMARY KEY) INSERT INTO testTable VALUES(1) INSERT INTO testTable VALUES(1) END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ERROR_ID, ERROR_MESSAGE() AS ERROR_MSG, ERROR_SEVERITY() AS ERROR_SEVERITY, ERROR_STATE() AS ERROR_STATE, ERROR_PROCEDURE() AS ERROR_PROCEDURE, ERROR_LINE() AS ERROR_LINE END CATCH GO SELECT * FROM testTable -- Contains single record with value 1 --// Test for recreating a table that already exists (There is already an object named 'testTable' in the databASe.) BEGIN TRY CREATE TABLE testTable (a INT PRIMARY KEY) END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ERROR_ID, ERROR_MESSAGE() AS ERROR_MSG, ERROR_SEVERITY() AS ERROR_SEVERITY, ERROR_STATE() AS ERROR_STATE, ERROR_PROCEDURE() AS ERROR_PROCEDURE, ERROR_LINE() AS ERROR_LINE END CATCH GO --// Test for inserting NULL value on Primary Key column (Cannot insert the value NULL into column 'a', table 'tempdb.dbo.testTable'; column does not allow nulls. INSERT fails.) BEGIN TRY INSERT INTO testTable VALUES(NULL) END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ERROR_ID, ERROR_MESSAGE() AS ERROR_MSG, ERROR_SEVERITY() AS ERROR_SEVERITY, ERROR_STATE() AS ERROR_STATE, ERROR_PROCEDURE() AS ERROR_PROCEDURE, ERROR_LINE() AS ERROR_LINE END CATCH GO --// Final Cleanup DROP TABLE testTable DROP PROC testPrc GO
MS BOL Links for TRY-CATCH:
http://msdn.microsoft.com/en-us/library/ms175976.aspx
http://msdn.microsoft.com/en-us/library/ms179296%28v=SQL.90%29.aspx
http://msdn.microsoft.com/en-us/library/ms179495.aspx (Error Information)
-
July 29, 2011 at 9:52 am | #1TSQL Interview Questions – Part 1 « SQL Server Programming, Tips & Tricks
-
December 1, 2011 at 7:25 am | #2XACT_ABORT with TRANSACTIONS « SQL Server Programming, Tips & Tricks
-
February 26, 2012 at 7:29 am | #3Best Practices while creating Stored Procedures « SQL Server Programming, Tips & Tricks
-
November 19, 2012 at 12:37 am | #4Passed 70-461 Exam : Querying Microsoft SQL Server 2012 « SQL with Manoj
-
December 20, 2012 at 7:07 pm | #5New THROW statement in SQL Server 2012 (vs RAISERROR) « SQL with Manoj
Leave a Reply Cancel reply
Download Win8 APP for SQLwithManoj HERE
SQL Tags
Categories
- BIDS (1)
- Datatypes (3)
- DB Concepts (19)
- DBA Task (14)
- Differences (21)
- Excel (6)
- Indexes (3)
- Integration Services (4)
- Interview Questions (6)
- JOINS (4)
- Microsoft (5)
- Misconception (1)
- MS SQL SERVER 2005 (8)
- MS SQL SERVER 2008 (8)
- MS SQL Server Denali (23)
- MS.net (1)
- New Features (4)
- Optimization Performance (8)
- SQL DB Engine (1)
- SQL Errors (4)
- SQL Tips (47)
- Stored Procedures (9)
- tempdb (3)
- Uncategorized (6)
- VBA Macro (1)
- web apps (1)
- Windows (1)
- XML (13)
Archives
- May 2013 (2)
- April 2013 (2)
- February 2013 (2)
- January 2013 (2)
- December 2012 (4)
- November 2012 (3)
- October 2012 (3)
- September 2012 (5)
- August 2012 (1)
- July 2012 (5)
- June 2012 (1)
- May 2012 (4)
- April 2012 (7)
- March 2012 (1)
- February 2012 (2)
- January 2012 (3)
- December 2011 (11)
- November 2011 (1)
- October 2011 (6)
- September 2011 (3)
- August 2011 (2)
- July 2011 (12)
- June 2011 (2)
- May 2011 (3)
- April 2011 (1)
- March 2011 (5)
- February 2011 (9)
- January 2011 (7)
- December 2010 (9)
- November 2010 (6)
- October 2010 (4)
- September 2010 (3)
- August 2010 (3)
- July 2010 (1)
- June 2010 (1)
- May 2010 (1)
- March 2010 (1)
- February 2010 (1)
- January 2010 (1)
- December 2009 (1)
- November 2009 (1)
- September 2009 (2)
- August 2009 (1)
- April 2009 (2)
- March 2009 (2)
- February 2009 (3)
Top Posts
- Passed 70-461 Exam : Querying Microsoft SQL Server 2012
- TSQL Interview Questions - Part 1
- Using OUTPUT Parameters in Stored Procedures
- CUBE, ROLLUP, COMPUTE, COMPUTE BY, GROUPING SETS
- SQL Jokes!!!
- SQL Server 2012 does not support Linked Server to SQL Server 2000, workaround
- UPDATE statement with new .WRITE Clause
- Difference between TRUNCATE, DELETE and DROP?
- SELECT an XML string to a table
- Logging in SSIS... using SQL Server Log Provider
Top Rated
Blog Stats
- 347,773 hits
StatCounter …since April’2012
Disclaimer
This is my personal blog site.
The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MS Books On Line and/or MSDN/TechNet.
My employer do not endorse any tools, applications, books, or concepts mentioned on the blog. I have documented my personal experience on this blog.



