I am experiencing some behavior with
THROW that I can't understand. Consider the following stored procedure:
CREATE PROCEDURE usp_division_err AS SET NOCOUNT ON; BEGIN TRY EXEC('select 1/0') END TRY BEGIN CATCH THROW; END CATCH
When the procedure is executed, the following error is raised:
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
Note, no information about in which procedure the error was raised is included. That's because the erroneous, dynamic SQL is executed in another scope, and that is fine. However, change the
CATCH-block to look like this
BEGIN CATCH THROW 50000, 'An error occurred.', 1; END CATCH
and the execution of the procedure will raise this error instead:
Msg 50000, Level 16, State 1, Procedure usp_division_err, Line 7 [Batch Start Line 0]
An error occurred.
The error is still encountered while executing the dynamic SQL, but when I manually specify the error number and error message (the first and second parameter of
THROW), the procedure-name of the executing procedure somehow appears.
Why does the procedure-name appear in the second error-message but not the first?