Updated March 16, 2023
Introduction to T-SQL TRY CATCH
T-SQL TRY…CATCH is defined as, it is a block that can be utilized to assess the position of activities in which we can say that it has been built to authorize us to control the exceptions in T-SQL neatly; for making a TRY CATCH block, first, we have to set a group of T-SQL statements if we have statements between TRY and CATCH and if the statements in the TRY block do not have an error then the CATCH block will not be able to implement, and if the TRY block has an error, then the control has been hand over in the CATCH block.
What is T-SQL TRY CATCH?
The TRY CATCH block in the T-SQL statements assists in managing the errors, which are the same as we can handle the exception in other programming languages, as the syntax of the TRY CATCH statement allows us to write several statements inside the CATCH block, so if TRY block observes an error then it departs from the try block and gets entered into the CATCH block. It can implement the statements available in the CATCH block, and afterward, it will give back the related error illustration.
The TRY CATCH block has been utilized for managing the errors, and it has been introduced by Microsoft with the help of syntax, as the syntax given below can handle the error conditions.
TRY CATCH Syntax:
BEGIN TRY -----statements END TRY BEGIN CATCH -----statements END CATCH
From the above syntax, we have to keep a list of things in mind, such as:
- Every TRY block has ended with the CATCH block in which we can not write anything between the END TRY and BEGIN CATCH.
- If the TRY block does not find any error, then that cannot be managed by the CATCH block, in which we can say that the controller can implement the statements after the END CATCH.
- If the TRY block can find an error, then the control of the TRY block has been directly gone into the CATCH block.
- The T-SQL TRY CATCH can capture all the severe errors.
- The TRY CATCH block can allow us to use nested TRY blocks so that the TRY CATCH can be inside another block.
- And the CATCH block cannot manage the syntax errors mean errors we can find at the compile time.
How to Add T-SQL TRY CATCH?
The TRY CATCH block in the T-SQL has been built to allow us to manage the exception neatly, and the try…catch block has been constructed for putting the set of statements that can cause the exception within BEGIN END TRY.
—–sample statements that cause the exception
Then directly, we can able to utilize the BEGIN CATCH…..END CATCH block following the TRY block.
—–sample statement that manages the exception
Let us see the illustration of the above two statements.
—–sample statements that cause the exception
—–sample statements that manage the exception
The above statement has been illustrated as, if the statement between the TRY block has no error, then the CATCH block will not execute, but if the statement in the TRY block will cause the exception, then that exception has been managed by the CATCH block, so in this way, we can able to add the TRY CATCH block.
T-SQL TRY CATCH Error
Different errors are mentioned below:
- ERROR_NUMBER: It will give back the internal number of the error, which means the error number.
- ERROR_STATE: It will give back the state number of a T-SQL error which is the information about the source.
- ERROR_SEVERITY: It gives back the details about useful errors to the errors which users of the DBA can fix.
- ERROR_LINE: It gives back the line number concerning the error on it.
- ERROR_PROCEDURE: It gives back the name of the stored procedure or function.
- ERROR_MESSAGE: It gives back the essential details, which can be the error message text.
Examples of T-SQL TRY CATCH
Following are two examples in which the first is the simple TRY CATCH block, and the second is the TRY CATCH with transactions.
Let us see the example which exhibits a SELECT statement that can generate the divide-by-zero error, and the source of that error implementation can be thrown to the related CATCH block.
BEGIN TRY -- to create a divide-by-zero error. SELECT 2/0; END TRY BEGIN CATCH SELECT ERROR_NUMBER () AS FallacyNumber ,ERROR_SEVERITY() AS HarshError ,ERROR_STATE() AS ErrorPosition ,ERROR_PROCEDURE() AS ErrorProcess ,ERROR_LINE() AS ErrorString ,ERROR_MESSAGE() AS ErrorSMS; END CATCH; GO
In the above example, as we have the stored procedure in the TRY block, there is an error for which we have written the formula, and the CATCH block can handle it by using ERROR functions.
This is an example to exhibit the working of the try…catch block in the transaction where the statement in the TRY block can create a constraint contravention error.
BEGIN TRANSACTION; BEGIN TRY -- Generate a constraint contravention error. DELETE FROM Presentation.Present WHERE PresentID = 890; END TRY BEGIN CATCH SELECT ERROR_NUMBER () AS FallacyNumber ,ERROR_SEVERITY() AS HarshError ,ERROR_STATE() AS ErrorPosition ,ERROR_PROCEDURE() AS ErrorProcess ,ERROR_LINE() AS ErrorString ,ERROR_MESSAGE() AS ErrorSMS; IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; END CATCH; IF @@TRANCOUNT > 0 COMMIT TRANSACTION; GO
In the above example, we have the stored procedures in which we have utilized the transactions we try to commit in the CATCH block.
In this article, we conclude that the TRY…CATCH block has been utilized to manage the exceptions by following the specific syntax; we have also discussed how to add the T-SQL TRY…CATCH block, error in it, and also described the examples, so this article will help to understand the concept of TRY…CATCH.
This is a guide to T-SQL TRY CATCH. Here we discuss the introduction, how to add T-SQL TRY CATCH? and examples. You may also have a look at the following articles to learn more –