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 6066143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:28:09+00:00 2026-05-23T09:28:09+00:00

I made a catch, that if a specific sql server error is thrown, that

  • 0

I made a catch, that if a specific sql server error is thrown, that the code can dis-regard and carry on. A co-worker said that the error message I’m catching is being thrown from SQL and it may change when we upgrade to 2008. He said my solution will work today, but it is a weak solution. So, my question is…How should I catch and handle a error thrown from SQL server, if it may be dependend on the sql server version. ie(2005, 2008 or newer)? Any ideas on how to make the catch better?

My code

string sqlError = "The INSERT statement conflicted with the FOREIGN KEY constraint Table1. The conflict occurred in database Work1, table Table2.";

catch (Exception oExp)
{
//This check will allow the specific error to continue without getting caught.
if (oExp.Message.Contains(Constants.sqlError) == false)
{
  throw oExp;
}
}
  • 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-23T09:28:09+00:00Added an answer on May 23, 2026 at 9:28 am

    As noted in the comments, this is avoidable so you should do the right thing and make sure it can’t happen in the first place.

    And as you’ve surmised, using static text is a bad thing. This is even more true when you consider that these messages will change depending on the LANGUAGE that SQL Server is installed with or the user is logged on as (as SQL messages are locale sensitive), regardless of upgrade level.

    Also … I don’t quite understand why you’d want to continue even if this error is thrown as it sounds like it puts your data into an inconsistent state, so I’d review whether you’re doing the right thing in the first place. However I don’t know your application…

    So with all those caveats said … in the event that avoiding this is not possible (for whatever reason – excluding laziness 🙂 ), rather than catch a generic Exception, you should catch a SqlException, which includes the State and Number properties which you should be able to use verbatim.

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

Sidebar

Related Questions

I have some code that builds SQL queries to perform a lenient, catch-all kind
I made a code that translate strings to match each word from the array
I have implicitly made this a community wiki seeing that the answers can be
I made a class from Linq to SQL Clasees with VS 2008 SP1 Framework
I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I made a UIView using Interface Builder with a top bar that has Cancel
I'm trying to understand how to ensure that a specific action completes in a
I have a Java program that executes specific commands into the OS. Am also
I have a piece of code that may or may not fail, which I
i'm profiling the below code inside a singltone and found that a lot of

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.