Try and Catch Fun in Sql Server 2005

IntroductionAS
Try and Catch is very popular among the developerSELECT
community writing code in C#, C++, or other highERROR_NUMBER() AS ErrorNumber,
level languages. The conventional term referring toERROR_SEVERITY() AS ErrorSeverity,
Try-Catch blocks is Exception Handling. ExceptionERROR_STATE() AS ErrorState,
Handling is simply a breach of an application'sERROR_PROCEDURE() AS ErrorProcedure,
predefined assumptions. It enables us to provide aERROR_LINE() AS ErrorLine,
reliable data/process validation mechanism in ourERROR_MESSAGE() AS ErrorMessage;
applications. SQL Server did not have any closeGO
counterpart for it until now. Prior to SQL ServerLet us modify the first script slightly:
2005, many of us relied on the variable @@ERROR.Collapse
If there was any deviant behavior, then @@ERRORDeclare @deadline intset @deadline = 0
would capture a non-zero value to indicate the errorBEGIN TRY
code.SELECT DaysToManufacture / @deadlinefrom
RequirementAdventureWorks.Production.Product
Please make sure the following are available at hand:WHERE ProductID = 921
- SQL Server 2005 (any version listed here).END TRY
- AdventureWorks database (can be downloadedBEGIN CATCH
from Microsoft).EXECUTE usp_GetErrorInfo;
Keep in mind that AdventureWorks does not comeEND CATCH;
installed by default in the SQL Server Express edition.This outputs:
In short, AdventureWorks is a database for a
fictitious company. Sample examples from MicrosoftLet’s modify the second script to check its
utilize this database as a way to provide proof ofbehavior outside of the Catch block’s scope:
concept.Collapse
ImplementationDeclare @deadline intset @deadline = 0
Many of us may have seen something like theSELECT DaysToManufacture / @deadlinefrom
following as a way to inform errors:AdventureWorks.Production.Product
CollapseWHERE ProductID = 921
Declare @deadline intset @deadline = 0SELECT usp_GetErrorInfo;
SELECT DaysToManufacture / @deadlinefromThis outputs:
AdventureWorks.Production.Product
WHERE ProductID = 921if @@ERROR <>So far, a variety of ways to handle exceptions has
0beginprint 'Error occurred'endbeen covered. In SQL Server 2005, it is possible to
This outputs:work with nested Try..Catch blocks. This means that
Collapsewithin the scope of a Catch block, one could check
Msg 8134, Level 16, State 1, Line 1whether the logic to cover for predefined cases is
Divide by zero error encountered.breached again. Modifying the earlier query gets to
Error Occurredmake it look something like:
For the most part, the above works fine, but it’sCollapse
not as robust as exception handling. It does not giveDeclare @deadline intset @deadline = 0
us the flexibility that a try..catch block construct does.BEGIN TRY
Let’s see how this would look in the currentSELECT DaysToManufacture / @deadlinefrom
world:AdventureWorks.Production.Product
CollapseWHERE ProductID = 921
Declare @deadline intset @deadline = 0END TRY
BEGIN TRYBEGIN CATCH
SELECT DaysToManufacture / @deadlinefromBEGIN TRYexecute usp_GetErrorInfoselect 'Error
AdventureWorks.Production.Productoccurred at: ' + GetDate() – format exception
WHERE ProductID = 921END TRY
END TRYBEGIN CATCHselect 'Error Occurred'
BEGIN CATCHprint 'Error Occurred'END CATCH;
END CATCH;END CATCH;
This outputs:This outputs:
Collapse
(0 row(s) affected)Conclusion
Error OccurredOne would wonder why anyone would bother adding
Does this mean @@ERROR goes away? No, one canthe extra bit of syntax. It seems too much
still get access to the error value contained instructured work and overhead to existing practices.
@@ERROR. However, SQL Server 2005 definesThe examples above that used exception handling
several functions whose value can be obtained onlyallowed the execution flow to run smoothly. If you
within the scope defined within Begin Catch...Endcompare the output between @@ERROR and
Catch. They are ERROR_NUMBER(),Try..Catch, then it is possible to notice that the SQL
ERROR_SEVERITY(), ERROR_STATE(),Server manager didn’t abruptly go to the tab
ERROR_PROCEDURE(), ERROR_LINE(),showing the warning. Instead, it showed the result
ERROR_MESSAGE(). BOL or Books Online has aset that one could easily pick up on the application
helper procedure namely usp_GetErrorInfo whichside. Also, try to see it this way a wise man once
gets the error related information for us. Latersaid, “Brakes are put in cars so that one could
examples from this article will utilize this procedure.drive freaking fast”. Exception handling in SQL
CollapseServer 2005 is here to help.
CREATE PROCEDURE usp_GetErrorInfo