We generally throw exceptions in this way:
RAISERROR (@ErrorMsg, 16, 1)
1, if we add the Try Catch module to the stored procedure, then when the execution reaches RAISERROR, it will jump directly to the Catch module.
For example:
DECLARE @ErrorMsg VARCHAR(2000) BEGIN TRY SELECT 1 RAISERROR ('reported an error', 16, 1) SELECT 2 END TRY BEGIN CATCH SELECT @ErrorMsg = ERROR_MESSAGE() RAISERROR (@ErrorMsg, 16, 1) END CATCH
The above code will not execute Select 2
2, if we do not add the Try Catch module in the stored procedure.
For example:
SELECT 1 RAISERROR ('reported an error', 16, 1) SELECT 2
The execution order of the above code is:
Select 1
throws an exception
Select 2
Here Select 2 does not need to be executed in most cases,
so we can write it like this
SELECT 1 RAISERROR ('reported an error', 16, 1) RETURN SELECT 2
But we recommend using the Try Catch module as much as possible