Issues with T-SQL TRY CATCH?

12,792

Solution 1

TRY ... CATCH doesn't catch every possible error but the ones not caught are well documented in BOL Errors Unaffected by a TRY…CATCH Construct

TRY…CATCH constructs do not trap the following conditions:

  • Warnings or informational messages that have a severity of 10 or lower.
  • Errors that have a severity of 20 or higher that stop the SQL Server Database Engine task processing for the session. If an error occurs that has severity of 20 or higher and the database connection is not disrupted, TRY…CATCH will handle the error.
  • Attentions, such as client-interrupt requests or broken client connections.
  • When the session is ended by a system administrator by using the KILL statement.

The following types of errors are not handled by a CATCH block when they occur at the same level of execution as the TRY…CATCH construct:

  • Compile errors, such as syntax errors, that prevent a batch from running.
  • Errors that occur during statement-level recompilation, such as object name resolution errors that occur after compilation because of deferred name resolution.

These errors are returned to the level that ran the batch, stored procedure, or trigger.

Solution 2

There was one case in my experience when TRY...CATCH block didn't catch the error. There was error connected with collation: Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "Latin1_General_CI_AI" in the equal to operation. Maybe this error correspond one of the error type documented in BOL.

Errors that occur during statement-level recompilation, such as object name resolution errors that occur after compilation because of deferred name resolution.

Solution 3

TRY ... CATCH will fail to catch an error if you pass a "bad" search term to CONTAINSTABLE

For example:

DECLARE @WordList VARCHAR(800)
SET @WordList = 'crap"s'
CON

TAINSTABLE(table, *, @WordList)

The CONTAINSTABLE will give you a "syntax error", and any surrounding TRY ... CATCH does not catch this.

This is particularly nasty because the error is caused by data, not by a "real" syntax error in your code.

Share:
12,792
Jim Evans
Author by

Jim Evans

Updated on June 08, 2022

Comments

  • Jim Evans
    Jim Evans about 2 years

    We are currently on SQL 2005 at work and I am migrating an old Foxpro system to new web application backed by SQL Server. I am using TRY CATCH in T-SQL for transaction processing and it seems to be working very well. One of the other programmers at work was worried about this as he said he had heard of issues where the catch phrase did not always catch the error. I have beat the sproc to death and cannot get it to fail (miss a catch) and the only issues I have found searching around the net is that it will not return the correct error number for error numbers < 5000. Has anyone experienced any other issues with TRY CATCH in T-SQL - especially if it misses a catch? Thanks any input you may wish to provide.

  • Jim Evans
    Jim Evans about 13 years
    I asked a general question if anyone had experienced any issues with TRY CATCH - I did not ask anyone to prove anything.
  • David Grenier
    David Grenier about 10 years
    -1, T-SQL Try Catch not handling CLR integration errors. However, the question was useful and the other answer informative.