locked
How catch exception codes / error code in C# RRS feed

  • Question

  • User1408093167 posted
    I want to have this structure in my code but when i get a exception i need the error code of the exception, but i think that´s not possivel, anyone have some idea to help me?
    try
    {
    }
    catch(FileNotFoundException ex)
       errorCode = ex. ?????
    }
    catch(SqlException ex)
    {
       errorCode = ex.Number;
    }


    For example:

    <EXCEPTIONS>
       <LANGUAGE lang="PT">
          <EXCEPTION tipo="sqlexception">
              <ERROR ID="4060">
                 <DESCRIPTION>The server is not operational</DESCRIPTION>
                 <TRANSLATION>O servidor nao esta operacional</TRANSLATION>
             </ERROR>
         </EXCEPTION >
       </LANGUAGE>
    </EXCEPTIONS>

    With this ,i catch a exception and them do translations, but i really need the error code, without the error code, nothing done.
    Do you have any idea?

    Thanks in advance.
    Thursday, November 3, 2005 2:31 PM

All replies

  • User1886108404 posted

    I'd like to catch only SqlException with a specific ErrorCode:

    try
    {
    	// some operation causing a SqlException
    }
    catch (SqlException e)
    {
    	// avoid [SqlException (0x80131904): Transaction (Process ID ?) was deadlocked on lock resources with
    	// another process and has been chosen as the deadlock victim. Rerun the transaction.]!
    	if (e.ErrorCode == 0x80131904)
    		return null;
    
    	throw;
    }


    But if (e.ErrorCode == 0x80131904) will not work. If I always return null in case of the exception it will work. But quering ErrorCode from the e object causes that the exception will be always rethrown ....

    Friday, March 19, 2010 10:33 AM
  • User1886108404 posted

    Got it!

    The solution is that you should use e.Number not e.ErrorCode:

    try
    {
    	...
    }
    catch (SqlException e)
    {
    	// Avoid [SqlException (0x80131904): Transaction (Process ID ?) was deadlocked on lock resources with
    	// another process and has been chosen as the deadlock victim. Rerun the transaction.]!
    
    	// CAUTION: Using e.ErrorCode and testing for HRESULT 0x80131904 will not work!
    	if (e.Number != 1205) // only catch deadlocks
    		throw;
    
    	return null;
    }

    ;-)


    Friday, March 19, 2010 11:00 AM
  • User-1636183269 posted

    Your code is correct, but it will go only onc exception, so if first one execute then second catch will not occur. Just debug and verify it. 

    Friday, March 19, 2010 11:08 PM
  • User1886108404 posted

    Thank you for your reply but I am afraid I don't unserstand your answer ... Innocent

    It depends who/where calls that code scnipped I posted here.

    In our case: The user clicks a link in the UI which causes the execution of a SQL search at the end (at the business layer):

    protected virtual TypedDomainObjectCollection<TObjectClass> ExecuteSearch()
    {
    	for (var i = 0; i < 3; i++)
    	{
    		var result = ExecuteSearchPrivate(); // <--- try/catch from original post
    
    		if (result == null)
    			System.Threading.Thread.Sleep(250);
    		else
    			return result;
    	}
    
    	return null;
    }

    The purpuse just was: If a SQL deadlock occurs, try execute the search a second and third time again. The exception says "Rerun the transaction" and so we have done that ;-)

    If another problem/Exception occurs this should still cause an exception or if the third time the transaction still causes a deadlock an exception it should be thrown also of course.


    Monday, March 22, 2010 7:09 AM