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 - SQL Server Tutorial
From the course: Program Databases with Transact-SQL
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
What is error handling?1m 52s
-
(Locked)
Capture errors with TRY and CATCH4m 54s
-
(Locked)
Generate errors with THROW4m 12s
-
(Locked)
Manage transaction control4m 4s
-
(Locked)
Challenge: Add error handling to a stored procedure1m 12s
-
(Locked)
Solution: Add error handling to a stored procedure12m 12s
-
(Locked)
-
-