TRY/CATCH
Statement
Executes the statement-to-try
statements until successful completion or until one of the statements throws an error. If an error is thrown, then
the statement-if-error statements are executed. The statements in the
CATCH
block may use the ERROR_MESSAGE
function to
access the thrown message.Syntax
BEGIN TRY[statement-to-try]
...
END TRY
BEGIN CATCH
[statement-if-error]
...
END CATCH
Parameters
-
statement-to-try: statement
The statements to execute. These statements mayTHROW
an error, causing execution to divert to the statement-if-error statements. -
statement-if-error: statement
The statements to execute if any statement-to-try throws an error. These statements may use theERROR_MESSAGE
function to access the thrown message.
Example
-- Prints "Foo".
BEGIN TRY
THROW 'Foo';
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE();
END CATCH