Results 1 to 8 of 8

Thread: Raiserror?

  1. #1
    Join Date
    Jan 2008
    Posts
    186

    Unanswered: Raiserror?

    as many of you know, i'm fairly new to DB programming, and never really used RAISERROR before.

    Instead, I would attach output variables called "returnCode" and "returnMessage" to each stored proc. And if there was an error, I would set "returnCode = 1" in the sproc, and set "returnMessage" to a custom message.

    Then, in middle-layer DLL, I would check if returnCode is non-zero, and if it is, I would throw an exception with the specified returnCode and returnMessage.

    Can anyone tell me the advantage of using RAISERROR over the approach I'm currently using? IS there a significant advantage, in any respect?

    Thanks

  2. #2
    Join Date
    Sep 2005
    Posts
    161
    I'll start with one advantage. Feel free to add to the list.

    1. If you do not have control over the middleware, you can use RAISERROR to force an error that must be dealt with by the middleware.

  3. #3
    Join Date
    Jan 2008
    Posts
    186
    Well, here's why I'm doing it that way, and maybe you could comment on it.

    If I manually set returnCode and returnMessage, I can throw my own exeption upon checking this in my DLL. In other words, it's an known-type of error, and I can throw a "KnownErrorException"

    Now, it's possible that my sprocs might generate other bugs/errors that I don't know about. In this case, the standard SqlException will be thrown

    Bottom line: I can distinguish between the errors I throw, versus the ones that I don't (and then possibly I can turn those into "known" errors if need be)

    What do you think?

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by dbguyfh
    Bottom line: I can distinguish between the errors I throw, versus the ones that I don't (and then possibly I can turn those into "known" errors if need be)
    How does that not apply to errors thrown with raiserror?

  5. #5
    Join Date
    Jan 2008
    Posts
    186
    Quote Originally Posted by pootle flump
    How does that not apply to errors thrown with raiserror?
    Well, for example, whether or not the error is genereated by RAISERROR or some other means, it will still throw a SqlException. So my question is: How can I distinguish whether or not the SqlException was a result of my own RAISERROR statement? Or via some other means?

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Errors raised by the Microsoft SQL engine have numbers less than 50000. User errors start at 50000 and go up from there.

    -PatP

  7. #7
    Join Date
    Jan 2008
    Posts
    186
    Quote Originally Posted by Pat Phelan
    Errors raised by the Microsoft SQL engine have numbers less than 50000. User errors start at 50000 and go up from there.

    -PatP
    Thanks for the reply.

    Which property does that refer to? The "ErrorCode" or the "Number" property?

  8. #8
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    Errorcode is the one that contains the value of explicit or implicit RETURN from the proc. Errornumber has the value of 50001 caused by implicitly assigning the message ID to the custom message you generate by RAISERROR.
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •