Raising and Throwing Errors

Learn to generate custom error messages.

There are times when we know that our stored procedures or functions might generate an error. In these cases, we can raise custom errors that have readable messages.

The RAISERROR() function

We can intentionally raise an error in our query using the RAISERROR() function. It has this syntax:

RAISERROR([Message], [Severity], [State])

Example

When this function is called, the execution is transferred to the error processing block. In other words, if the RAISERROR() function is called within the BEGIN TRY block, the execution switches to the BEGIN CATCH block:

Get hands-on with 1200+ tech skills courses.