From the course: Program Databases with Transact-SQL

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Capture errors with TRY and CATCH

Capture errors with TRY and CATCH - SQL Server Tutorial

From the course: Program Databases with Transact-SQL

Start my 1-month free trial

Capture errors with TRY and CATCH

- [Instructor] In your SQL server programs, error handling is most often implemented using Try and Catch blocks. The basic syntax is fairly simple. I've got an outline of what that looks like starting on line six and going down to line number 12. At the beginning of the routine, that you want to execute, add BEGIN TRY, then include all of the code you want to run. Assuming that it executes without any problems. After that, you'll have a line that says END TRY followed immediately by a BEGIN CATCH. If any errors are encountered in the TRY block, then the code in the CATCH block is executed. If the TRY block doesn't encounter any errors, then this portion is skipped over, finish the CATCH block with END CATCH . And then you can continue on with other code you want to execute after that. To put this into practice, let's try and execute an insert statement where we don't supply the values that we need for the required fields in a table. I've got that outlined here on line number 18, where…

Contents