In my stored procedures, when a business rule is broken I raise an error that bubbles up to the C# client app and gets displayed to the user. e.g.
RAISERROR('Hey, you cannot do that because blah blah blah', 16, 1);
I want to distinguish errors that I raise from other SQL Server errors, because I only want my errors to get displayed.
I think these are the only ways to send a message to the client app that this is a user message to display: Severity level, State, Return Code. But I think I should leave the severity level.
- How to I tell my client app?
- And what code or number should I use that is not already being used?
- Or is there another way I have not considered?
Or should I do this:
THROW 50000, 'Hey, you cannot do that!', 1;
EDIT (13-April-2022) I asked Erland Sommarskog whose response helped me realise that if you use this
RAISERROR('Hey, you cannot do that because blah blah blah', 16, 1);
...then the client app will always get 50000 as the error number. And with RAISERROR you can use parameterized messages. You just need to be aware that RAISERROR will not exit the batch. So, for me, RAISERROR wins.