Advertisement
Intermediate SQL Server, Error Handling with TRY CATCH

What types of errors cannot be caught by a TRY CATCH block in SQL Server?

Short answer

TRY CATCH cannot catch certain severe errors that terminate the database connection entirely, such as a fatal error causing the session to disconnect, compile time syntax errors that prevent the batch from even starting, or certain very low severity warnings that do not actually…

Save SQL Server mock interview
Advertisement
AModel answer
TRY CATCH cannot catch certain severe errors that terminate the database connection entirely, such as a fatal error causing the session to disconnect, compile time syntax errors that prevent the batch from even starting, or certain very low severity warnings that do not actually raise a catchable error condition at all.
-- A syntax error prevents the batch from compiling
-- and cannot be caught by TRY CATCH, since it never actually starts running
BEGIN TRY
  SELEC * FROM Orders; -- typo causes a compile error
END TRY
BEGIN CATCH
  PRINT 'This will never run';
END CATCH;
Real-world example
A developer is confused when a TRY CATCH block does not catch an error, only to realize it was actually a compile time syntax error in their SQL statement, which prevents the code from ever reaching the point where TRY CATCH could intervene.
Likely follow-ups
  • How do you catch a compile time error before it ever reaches production?
  • What severity level of error causes a connection to be terminated entirely?
Related topics