Press "Enter" to skip to content

RAISERROR vs THROW

Andy Brownsword looks at the two ways to bubble up an error in SQL Server:

I don’t use RAISERROR often – I usually forget which severity code to use. After looking at a sprinkling of them recently I decided it was time for a refresher, so come along for the ride.

If you check out the online documentation it states that “New applications should use THROW instead”. It also sounds like its used to raise ‘RROR’s (whatever they are?). Neither are quite the whole story though. Let’s get into it.

My general rule of thumb is that I tend to use THROW most of the time, but RAISERROR in loops so that I can print out how far along in the process something is, as there is no WITH NOWAIT equivalent to THROW. Andy mentions using THROW; without additional parameters, and that’s very helpful when you want to maintain the original error message rather than wrapping your own around it. It’s not quite as useful as a re-throw in a language like C#, where you keep stack trace information, but helps with troubleshooting.

As for not doubling the letter if it is the last letter of the first word and first letter of the second word (raise error or help protect), it was the fashion at the time, like wearing a yellow onion on your belt. I suppose the intent was to prevent typos or make it look slightly better, but I’ve never been a fan.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.