<p>I need to specifically catch SQL server timeout exceptions so that they can be handled differently.  I know I could catch the SqlException and then check if the message string Contains "Timeout" but was wondering if there is a better way to do it?</p>

<pre><code>try
{
    //some code
}
catch (SqlException ex)
{

    if (ex.Message.Contains("Timeout"))
    {
         //handle timeout
    }
    else
    {
         throw;
    }
}
</code></pre>

[{'answer_id': 29666, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 0, 'selected': False, 'text': '<p>Whats the value for the SqlException.ErrorCode property? Can you work with that?</p>\n\n<p>When having timeouts, it may be worth checking the code for <strong>-2146232060</strong>.</p>\n\n<p>I would set this up as a static const in your data code.</p>\n'}, {'answer_id': 62688, 'author': 'Jonathan', 'author_id': 6910, 'author_profile': 'https://Stackoverflow.com/users/6910', 'pm_score': 8, 'selected': True, 'text': '<p>To check for a timeout, I believe you check the value of ex.Number. If it is -2, then you have a timeout situation.</p>\n\n<p>-2 is the error code for timeout, returned from DBNETLIB, the MDAC driver for SQL Server. This can be seen by downloading <a href="http://www.red-gate.com/products/reflector/" rel="noreferrer">Reflector</a>, and looking under System.Data.SqlClient.TdsEnums for TIMEOUT_EXPIRED. </p>\n\n<p>Your code would read:</p>\n\n<pre><code>if (ex.Number == -2)\n{\n     //handle timeout\n}\n</code></pre>\n\n<p>Code to demonstrate failure:</p>\n\n<pre><code>try\n{\n    SqlConnection sql = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=YourServer,1433;Initial Catalog=YourDB;Integrated Security=SSPI;");\n    sql.Open();\n\n    SqlCommand cmd = sql.CreateCommand();\n    cmd.CommandText = "DECLARE @i int WHILE EXISTS (SELECT 1 from sysobjects) BEGIN SELECT @i = 1 END";\n    cmd.ExecuteNonQuery(); // This line will timeout.\n\n    cmd.Dispose();\n    sql.Close();\n}\ncatch (SqlException ex)\n{\n    if (ex.Number == -2) {\n        Console.WriteLine ("Timeout occurred");\n    }\n}\n</code></pre>\n'}, {'answer_id': 16005643, 'author': 'Roland Pihlakas', 'author_id': 193017, 'author_profile': 'https://Stackoverflow.com/users/193017', 'pm_score': 5, 'selected': False, 'text': '<p>here: <a href="http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.adonet/2006-10/msg00064.html" rel="noreferrer">http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.adonet/2006-10/msg00064.html</a>\n<br><br>\nYou can read also that <em>Thomas Weingartner</em> wrote:<br></p>\n\n<blockquote>\n  <p>Timeout: SqlException.Number == -2 (This is an ADO.NET error code)\n  <br>General Network Error: SqlException.Number == 11\n  <br>Deadlock: SqlException.Number == 1205 (This is an SQL Server error code)</p>\n</blockquote>\n\n<p>...<br></p>\n\n<blockquote>\n  <p>We handle the "General Network Error" as a timeout exception too. It only occurs under rare circumstances e.g. when your update/insert/delete query will raise a long running trigger.</p>\n</blockquote>\n'}, {'answer_id': 54634750, 'author': 'John Evans', 'author_id': 2255709, 'author_profile': 'https://Stackoverflow.com/users/2255709', 'pm_score': 4, 'selected': False, 'text': '<p>Updated for c# 6:</p>\n\n<pre><code>    try\n    {\n        // some code\n    }\n    catch (SqlException ex) when (ex.Number == -2)  // -2 is a sql timeout\n    {\n        // handle timeout\n    }\n</code></pre>\n\n<p>Very simple and nice to look at!!</p>\n'}, {'answer_id': 58621338, 'author': 'hamid reza Heydari', 'author_id': 11377678, 'author_profile': 'https://Stackoverflow.com/users/11377678', 'pm_score': 0, 'selected': False, 'text': '<p>I am not sure but when we have execute time out or command time out\nThe client sends an "ABORT" to SQL Server  then simply abandons the query processing. No transaction is rolled back, no locks are released. to solve this problem I Remove transaction in Stored-procedure and use SQL Transaction in my .Net Code To manage sqlException </p>\n'}, {'answer_id': 58844268, 'author': 'Srinivas Pakalapati', 'author_id': 1862075, 'author_profile': 'https://Stackoverflow.com/users/1862075', 'pm_score': -1, 'selected': False, 'text': '<p>When a client sends ABORT, no transactions are rolled back. To avoid this behavior we have to use  SET_XACT_ABORT ON\n<a href="https://learn.microsoft.com/en-us/sql/t-sql/statements/set-xact-abort-transact-sql?view=sql-server-ver15" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/sql/t-sql/statements/set-xact-abort-transact-sql?view=sql-server-ver15</a></p>\n'}]
['https://Stackoverflow.com/questions/29664', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2231/']