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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:58:32+00:00 2026-05-13T19:58:32+00:00

From a .NET 3.5 / C# app, I would like to catch SqlException but

  • 0

From a .NET 3.5 / C# app, I would like to catch SqlException but only if it is caused by deadlocks on a SQL Server 2008 instance.

Typical error message is Transaction (Process ID 58) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Yet, it does not seem to be a documented error code for this exception.

Filtering exception against the presence of the deadlock keyword in their message seems a very ugly way to achieve this behavior. Does someone know the right way of doing this?

  • 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-13T19:58:32+00:00Added an answer on May 13, 2026 at 7:58 pm

    The Microsft SQL Server-specific error code for a deadlock is 1205 so you’d need to handle the SqlException and check for that. So, e.g. if for all other types of SqlException you want the bubble the exception up:

    catch (SqlException ex)
    {
        if (ex.Number == 1205)
        {
            // Deadlock 
        }
        else
            throw;
    }
    

    Or, using exception filtering available in C# 6

    catch (SqlException ex) when (ex.Number == 1205)
    {
        // Deadlock 
    }
    

    A handy thing to do to find the actual SQL error code for a given message, is to look in sys.messages in SQL Server.

    e.g.

    SELECT * FROM sys.messages WHERE text LIKE '%deadlock%' AND language_id=1033
    

    An alternative way to handle deadlocks (from SQL Server 2005 and above), is to do it within a stored procedure using the TRY…CATCH support:

    BEGIN TRY
        -- some sql statements
    END TRY
    BEGIN CATCH
        IF (ERROR_NUMBER() = 1205)
            -- is a deadlock
        ELSE
            -- is not a deadlock
    END CATCH
    

    There’s a full example here in MSDN of how to implement deadlock retry logic purely within SQL.

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

Sidebar

Related Questions

I need to run the below script from my vb.net app & would like
I'm programmatically creating javascript files from a .NET web app and would like to
I'm thinking about converting an app from Asp.net to python. I would like to
I'm automating Visio 2003 from a VB.NET app. My code looks like this (with
I’m working on trying to port an ASP.NET app from Server 2003 (and IIS6)
My app reads an (html) file from my website, and I would like to
I'd like to attach to another process from a .Net console app and explore
I have a problem in my web forms ASP.NET app where I would like
I would like use from Spring.NET Aspect library Logging aspect together with log4Net. I
I just updated an app from .NET 2.0 to .NET 4.0 and I have

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.