0

I have this C# windows application. Exceptions occur at times. When an exception occurs, I want to drill down on the Error Code for that exception and then fetch and print it.

Posting sample code

public void CreateSkill(int row)
        {
            try
            {
                Excel1.MWMClient.MWMServiceProxy.Skill skill = new Excel1.MWMClient.MWMServiceProxy.Skill();
                skill.name = "perl";
               // skill.description = "bowler";
                skill.expertiseLevel = "3";
                skill.ID = 1;
                Console.WriteLine(skillClient.CreateSkill(skill));
                Console.WriteLine(skill.ID);
                ExcelRecorder(null, row);
            }
            catch (Exception ex)
            {
                ExcelRecorder(ex.Message, row);
            }
            finally
            {
                System.GC.Collect();
            }
        }

ExcelRecorder() is a method which prints the exception message in a cell in an excel sheet. I want to print the exception as well as the error code in my excel sheet. How do I fetch this error code through my code?

Posting an image

enter image description here

user1501034
  • 343
  • 3
  • 6
  • 16
  • There is a similar question [http://stackoverflow.com/questions/6893165/how-to-get-exception-error-code-in-c-sharp](http://stackoverflow.com/questions/6893165/how-to-get-exception-error-code-in-c-sharp) – Zen Sep 14 '12 at 08:25

1 Answers1

0

Try this one:

try { 

}
catch(Exception e) {
    int code = e.HResult;
}

or even:

try { 

}
catch (Exception e){ 
    var Wex = e as Win32Exception;
    int code =  Wex.ErrorCode;
};
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
RustamIS
  • 697
  • 8
  • 24