Microsoft Learn

THROW (Transact-SQL)

This article describes the Transact-SQL (T-SQL) `THROW` statement, which raises an error or exception.

Description

The `THROW` statement raises an error or exception. It is used in T-SQL code to signal an error condition.

Syntax


THROW [ ; ]
THROW integer_error_number,
      varchar_message,
      integer_state ;
            

Parameters

Usage

The `THROW` statement is a modern replacement for the `RAISERROR` statement in many scenarios. It simplifies error handling by providing a cleaner syntax for raising custom errors.

When `THROW` is executed without arguments within a CATCH block, it re-throws the original error that caused the CATCH block to be entered. This is useful for propagating errors up the call stack.

Examples

Example 1: Throwing a custom error

This example demonstrates how to throw a custom error with a specific error number, message, and state.


IF EXISTS (SELECT 1 FROM sys.objects WHERE name = 'MyTable' AND type = 'U')
BEGIN
    THROW 50001, 'Table MyTable already exists.', 1;
END
GO
            

Example 2: Re-throwing an error in a CATCH block

This example shows how to use `THROW` without arguments to re-throw an error caught by a CATCH block.


BEGIN TRY
    -- Code that might cause an error
    SELECT 1 / 0;
END TRY
BEGIN CATCH
    PRINT 'An error occurred. Re-throwing the original error...';
    THROW; -- Re-throws the caught error
END CATCH
GO
            

See Also