question

Wilfred van Dijk avatar image
Wilfred van Dijk asked

Try..Catch problem with RAISERROR

Hi, See this simple code: declare @a int = 1 declare @b int = 0 declare @c int begin try set @c = @a / @b end try begin catch raiserror ('ERROR occured' , 16,1) with NOWAIT --print 'ERROR occured' end catch If I run this code, it generates an error (as expected) and the "query is completed with errors". however, if I use the print statement instead of the RAISERROR statement, it still jumps into the catch block. However, the "query executed succesfully" Why is the raiserror variant different compared to the print statement? It has a severity of 16, which is not a value for an error (the code has several RAISERROR(..,16,1) WITH NOLOG fragments to display output)
t-sqlerror handling
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
anthony.green avatar image
anthony.green answered
TRY/CATCH bypasses the default error handling events. When using RAISERROR your re-importing the error handler When using PRINT your creating your own error handler, as the PRINT statement executed successfully, you get a query executed successfully message.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Wilfred van Dijk avatar image Wilfred van Dijk commented ·
Thanks for the info!. Figured out that severity 16 always returns "unsuccesful query"
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.