How to use try- catch statements in SQL Server?
How to use try- catch statements in SQL Server?
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Ashutosh Kumar Verma
17-Nov-2021Try-Catch Statement in SQL:
Implement for error handling of T-SQL is similar as the exception handling in Visual Studio C# and Visual Studio C++ language. A group of T-SQL statement is closed in try block in which error or exception can arises. And if any exception is generate in try block then it catch and handles by Catch block.
Syntax-
Execute the Errors in CATCH block:
The following system function can be used to obtain information about the error or exception that cause to execute the CATCH block,
ERROR_NUMBER(): Return the number of error or exceptions.
ERROR_SEVERITY(): Return the error severity.
ERROR_STATE(): Return the number of error state.
ERROR_PROCEDURE() : Return the name of that procedure or trigger in which error are generated.
ERROR_LINE(): Return the line number inside the routine where the error are generate.
ERROR_MESSAGE(): Return the complete text message about the error.
Following a stored procedure is create that generate error or exception and show that error in CATCH block,