How to get Exception Error Code in C#

前端 未结 5 1158
心在旅途
心在旅途 2020-11-30 08:57
try
{
     object result = processClass.InvokeMethod(\"Create\", methodArgs);
}
catch (Exception e)
{  
    // Here I was hoping to get an error code.
}
相关标签:
5条回答
  • 2020-11-30 09:06

    You can use this to check the exception and the inner exception for a Win32Exception derived exception.

    catch (Exception e) {  
        var w32ex = e as Win32Exception;
        if(w32ex == null) {
            w32ex = e.InnerException as Win32Exception;
        }    
        if(w32ex != null) {
            int code =  w32ex.ErrorCode;
            // do stuff
        }    
        // do other stuff   
    }
    

    Starting with C# 6, when can be used in a catch statement to specify a condition that must be true for the handler for a specific exception to execute.

    catch (Win32Exception ex) when (ex.InnerException is Win32Exception) {
        var w32ex = (Win32Exception)ex.InnerException;
        var code =  w32ex.ErrorCode;
    }
    

    As in the comments, you really need to see what exception is actually being thrown to understand what you can do, and in which case a specific catch is preferred over just catching Exception. Something like:

      catch (BlahBlahException ex) {  
          // do stuff   
      }
    

    Also System.Exception has a HRESULT

     catch (Exception ex) {  
         var code = ex.HResult;
     }
    

    However, it's only available from .NET 4.5 upwards.

    0 讨论(0)
  • 2020-11-30 09:09

    I suggest you to use Message Properte from The Exception Object Like below code

    try
    {
     object result = processClass.InvokeMethod("Create", methodArgs);
    }
    catch (Exception e)
    {  
        //use Console.Write(e.Message); from Console Application 
        //and use MessageBox.Show(e.Message); from WindowsForm and WPF Application
    }
    
    0 讨论(0)
  • 2020-11-30 09:17

    You should look at the members of the thrown exception, particularly .Message and .InnerException.

    I would also see whether or not the documentation for InvokeMethod tells you whether it throws some more specialized Exception class than Exception - such as the Win32Exception suggested by @Preet. Catching and just looking at the Exception base class may not be particularly useful.

    0 讨论(0)
  • 2020-11-30 09:27

    Building on Preet Sangha's solution, the following should safely cover the scenario where you're working with a large solution with the potential for several Inner Exceptions.

     try
     {
         object result = processClass.InvokeMethod("Create", methodArgs);
     }
     catch (Exception e)
     {
         // Here I was hoping to get an error code.
         if (ExceptionContainsErrorCode(e, 10004))
         {
             // Execute desired actions
         }
     }
    

    ...

    private bool ExceptionContainsErrorCode(Exception e, int ErrorCode)
    {
        Win32Exception winEx = e as Win32Exception;
        if (winEx != null && ErrorCode == winEx.ErrorCode) 
            return true;
    
        if (e.InnerException != null) 
            return ExceptionContainsErrorCode(e.InnerException, ErrorCode);
    
        return false;
    }
    

    This code has been unit tested.

    I won't harp too much on the need for coming to appreciate and implement good practice when it comes to Exception Handling by managing each expected Exception Type within their own blocks.

    0 讨论(0)
  • 2020-11-30 09:28

    Another method would be to get the error code from the exception class directly. For example:

    catch (Exception ex)
    {
        if (ex.InnerException is ServiceResponseException)
           {
            ServiceResponseException srex = ex.InnerException as ServiceResponseException;
            string ErrorCode = srex.ErrorCode.ToString();
           }
    }
    
    0 讨论(0)
提交回复
热议问题