SQL THROW Exception – Alternative to RAISERROR and Differences

Some time ago, I published an article on the RAISERROR command, which raises an exception and allows us to influence whether the script will or will not continue based on severity. The THROW command is an alternative command introduced with SQL Server 2012.

THROW Syntax in SQL Server

The syntax is simpler compared to RAISERROR. Here’s the syntax for both commands for reference:

  • RAISERROR (msg, severity, state)
  • vs
  • BEGIN TRY
  • SELECT 1/0
  • END TRY
  • BEGIN CATCH
  • THROW [ { error_number | @local_variable },
  • { message | @local_variable },
  • { state | @local_variable } ]
  • [ ; ]
  • END CATCH

While RAISERROR accepts 3 arguments, THROW can be called (but not necessarily) within a TRY/CATCH error handling construct. If you try to execute the THROW command on its own, it will result in an error:

THROW excepltion

Outside a TRY/CATCH block, you can use the command like this:

THROW 51000, ‘The record does not exist.’, 1;

Rate this post
Category: SQL Commands

About Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zednicek, and I have been working as a freelance Data Engineer for roughly 10 years. During this time, I have been publishing case studies and technical guides on this website, targeting professionals, students, and enthusiasts interested in Data Engineering particularly on Microsoft technologies as well as corporate finance and reporting solutions. 🔥 If you found this article helpful, please share it or mention me on your website or Community forum

Leave a Reply

Your email address will not be published. Required fields are marked *