It should be pointed out that, even if it were technically possible to alternate between THROW
and RAISERROR
, you (most likely) wouldn't want to actually do this. Why? Because the very nifty ability of the parameterless THROW
to re-throw the error using the same Message Number (i.e. Msg 8134
instead of Msg X
where X
>= 50000) isn't the only difference between them: THROW
is batch-aborting while RAISERROR
is not. This can be an important behavioral difference as demonstrated below.
Test Setup
--DROP PROC ##Throw;
--DROP PROC ##RaisError;
GO
CREATE PROCEDURE ##Throw
AS
SET NOCOUNT ON;
BEGIN TRY
SELECT 1/0 AS [DivideByZero];
END TRY
BEGIN CATCH
THROW;
END CATCH;
SELECT 1 AS [AA];
GO
CREATE PROCEDURE ##RaisError
AS
SET NOCOUNT ON;
BEGIN TRY
SELECT 1/0 AS [DivideByZero];
END TRY
BEGIN CATCH
RAISERROR('test, yo!', 16, 1);
-- RETURN; -- typically at end of CATCH block when using RAISERROR
END CATCH;
SELECT 2 AS [BB];
GO
Test 1
EXEC ##Throw;
SELECT 3 AS [CC];
Returns:
"Results" Tab:
DivideByZero
{empty result set}
"Messages" Tab:
Msg 8134, Level 16, State 1, Procedure ##Throw, Line 38
Divide by zero error encountered.
Test 2
EXEC ##RaisError;
SELECT 4 AS [DD];
Returns:
"Results" Tab:
DivideByZero
{empty result set}
BB
2
DD
4
"Messages" Tab:
Msg 50000, Level 16, State 1, Procedure ##RaisError, Line 45
test, yo!
To be fair, it is possible to mask this difference by doing the following:
- Always wrap all calls to code using
THROW
within a TRY...CATCH
construct (demonstrated below)
- Never place code after the
THROW
(well, except for END CATCH;
)
Test 3
BEGIN TRY
EXEC ##Throw;
SELECT 5 AS [EE];
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS [ErrorNumber], ERROR_MESSAGE() AS [ErrorMessage];
END CATCH;
SELECT 6 AS [FF];
GO
Returns:
"Results" Tab:
DivideByZero
{empty result set}
ErrorNumber ErrorMessage
8134 Divide by zero error encountered.
FF
6
Test 4
BEGIN TRY
EXEC ##RaisError;
SELECT 7 AS [GG];
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS [ErrorNumber], ERROR_MESSAGE() AS [ErrorMessage];
END CATCH;
SELECT 8 AS [HH];
GO
Returns:
"Results" Tab:
DivideByZero
{empty result set}
ErrorNumber ErrorMessage
50000 test, yo!
HH
8