Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3224712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:15:19+00:00 2026-05-17T16:15:19+00:00

I have an existing application which uses MS SQL stored procedures to enforce some

  • 0

I have an existing application which uses MS SQL stored procedures to enforce some business rules. When an error is detected it is raised as an exception using RAISERROR back to my .Net application.

The .Net application can then use Try/Catch blocks to catch and exceptions and perform and business logic. The problem is that there are several business rules validated in a single stored procedure. Which could raise different exceptions. What is the best way to capture these SQL exceptions and convert them to custom .Net Exception handlers.

For example my stored procedure may throw an exception for RuleA and RuleB. In my .Net code I can only capture SqlException. My custom error message for either RuleA or RuleB is returned in the SqlException inner exception. I could parse the Message string but this is UGLY and if someone changes the implementation in the stored proc. my logic would not pick it up.

What is a preferred method to translate the generic SqlException into MyRuleAException or MyRuleBException?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:15:19+00:00Added an answer on May 17, 2026 at 4:15 pm

    Normally the way to do it is to define the error constants in your .Net code and then you can check the value in your exception handling code. You can use constants to make the code more readable, something like this:

    /// <summary>
    /// Represents the error code returned from stored procedure when entity could not be found.
    /// </summary>
    private const int SQL_ERROR_CODE_ENTITY_NOT_FOUND = 50001;
    
    /// <summary>
    /// Represents the error code returned from stored procedure when entity to be updated has time mismatch.
    /// </summary>
    private const int SQL_ERROR_CODE_TIME_MISMATCH = 50002;
    
    /// <summary>
    /// Represents the error code returned from stored procedure when a persistence exception occurs (ex.
    /// billing flag is invalid, child records exist which prevent a delete, etc.).
    /// </summary>
    private const int SQL_ERROR_CODE_PERSISTENCE_ERROR = 50003;
    

    Then, you can handle the exceptions like this, and it makes your code much more readable and maintainable:

        if (e.InnerException is SqlException)
        {
            // verify exception code from SP and throw proper exception if required
            var sqlException = (SqlException)e.InnerException;
            if (sqlException.Number == SQL_ERROR_CODE_ENTITY_NOT_FOUND)
            {
                e = new EntityNotFoundException(e.Message, e);
            }
            else if (sqlException.Number == SQL_ERROR_CODE_TIME_MISMATCH)
            {
                e = new EntityTimestampMismatchException(e.Message, e);
            }
            else if (sqlException.Number == SQL_ERROR_CODE_PERSISTENCE_ERROR)
            {
                e = new EntityServicePersistenceException(e.Message, e);
            }
        }
    

    This is about as clean as you can make it in my opinion, but it’s still ok because you define the error codes in one place, so if anything ever changes, you just change the one constant.

    And to raise the error, you can do something like this in T-SQL:

     -- record wasn't found, raise an error
     DECLARE @l_error NVARCHAR(1000)
     SET @l_error = 'Record with ' + @p_IdFieldName + ' = ' + CONVERT(VARCHAR(128), @p_id)
        + ' does not exist in table [' + @p_TableName + ']'
     EXEC sp_addmessage @msgnum=50001, @severity=16, @msgtext=@l_error, @replace='replace'
     RAISERROR(50001, 16, 1)
    

    The 50001 represents the error number that will be in the SqlException.Number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.