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:
Outside a TRY/CATCH block, you can use the command like this:
THROW 51000, ‘The record does not exist.’, 1;