TRY/CATCH Doesn’t Always Work.


If you’re using TRY/CATCH to do exception handling in T-SQL, you need to be aware that there are a lot of things it doesn’t catch. Here’s a quick example.

Let’s set up two tables – bookmarks, and a process log to track whether our stored proc is working or not:

DROP TABLE IF EXISTS dbo.Bookmarks; DROP TABLE IF EXISTS dbo.ProcessLog; CREATE TABLE dbo.Bookmarks( URL VARCHAR(50)); GO CREATE TABLE dbo.ProcessLog( ProcessDate DATETIME, StatusMessage VARCHAR(50)); GO

And create a simple stored procedure that adds a bookmark, and tracks whether it was successful:

CREATE OR ALTER PROC dbo.AddBookmark @URL VARCHAR(50) AS BEGIN BEGIN TRY