In the program, sometimes there are errors and exception messages when completing some Transact-SQL. If we want to handle these abnormal information ourselves, we need to capture them manually. Then we can do it with trycatch.
Try it? CATCH construction includes two parts: TRY block and CATCH block. If an error condition is detected in the Transact-SQL statement contained in the TRY block, control is passed to the CATCH block, where the error can be handled.
After the CATCH block handles the exception, control will be passed to the first Transact-SQL statement after the ENDCATCH statement. If the ENDCATCH statement is the last statement in a stored procedure or trigger, control will return to the code that called the stored procedure or trigger. Transact-SQL statements after the wrong statement is generated in the TRY block will not be executed.
If there are no errors in the TRY block, control will be passed to the statement immediately after the associated ENDCATCH statement. If the ENDCATCH statement is the last statement in a stored procedure or trigger, control will be passed to the statement calling the stored procedure or trigger.
The TRY block begins with a BEGINTRY statement and ends with an ENDTRY statement. You can specify one or more Transact-SQL statements between the BEGINTRY and ENDTRY statements. The CATCH block must follow the TRY block. A CATCH block begins with a BEGINCATCH statement and ends with an ENDCATCH statement. In Transact-SQL, each TRY block is associated with only one CATCH block.