SQL Server 2012 begins by introducing a throw clause to replace the RAISERROR that has been used since SQL Server. Since the function is the same, it is all in try ... What is the difference between catch code blocks that do not catch errors and then throw errors?
RAISERROR statement |
THROW statement |
If a msg_id is passed to RAISERROR, the ID must being defined in sys.messages. |
The Error_number parameter does not has the to is defined in sys.messages. |
The MSG_STR parameter can contain printf formatting styles. |
The message parameter does not accept printf style formatting. |
The severity parameter specifies the severity of the exception. |
There is no severity parameter. The exception severity is always set to 16. |
The table above lists their differences
The biggest advantage of throw vs. RAISERROR is that we no longer need to capture the values of several system functions, such as error_message (), Error_state (), in the catch block, and then assign the values to the variables. Only a throw statement is required to complete the process where the original RAISERROR need to assign a variable and pass the argument to the throw exception. Also, the capture of the error line points directly to the error line, rather than the line of code that occurs when the RAISERROR statement takes place like RAISERROR. And throw can be as raiserror as can be passed, the effect and RAISERROR are the same.
The summary is that RAISERROR can do the throw can be done, and throw can also save RAISERROR the original need to complete a number of things. And Microsoft is also recommended to replace RAISERROR with throw.
-- Using THROW-1 BEGIN SELECT1/0 as dividebyzeroEND TRY BEGIN CATCH THROW; END CATCH GO
Results
(08134level1 6by zero error Encountered.
And if you use RAISERROR
Use [Jerrydb]GO--Using RAISERROR ()DECLARE @ERR_MSG as NVARCHAR(4000) ,@ERR_SEV as SMALLINT ,@ERR_STA as SMALLINT BEGINTRYSELECT 1/0 asDivideByZeroENDTRYBEGINCATCHSELECT @ERR_MSG =error_message (),@ERR_SEV =error_severity (),@ERR_STA =error_state ()SET @ERR_MSG= 'Error occurred while retrieving the data from database:' + @ERR_MSG RAISERROR(@ERR_MSG,@ERR_SEV,@ERR_STA) withNOWAITENDCATCHGO
Results
(050000level1 from Database by zero error encountered.
Reference:
New THROW statement in SQL Server (vs RAISERROR)
SQL Server->> throw sentence vs. RAISERROR sentence