C# Exception HResult

In this chapter you will learn:

  1. Get to know Exception.HResult
  2. Syntax for Exception.HResult
  3. Example - Exception.HResult

Description

Exception HResult gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

Syntax

Exception.HResult has the following syntax.


public int HResult { get; protected set; }

Example

The following code example defines a derived Exception class that sets the HResult property to a custom value in its constructor.


using System;//from   ww  w.ja v a2s  .c o m
using System.Collections;

class Sample 
{
   public static void Main()
   {
      try {
          Exception e = new Exception("This statement is the original exception message.");
          string s = "Information.";
          int i = -903;
          DateTime dt = DateTime.Now;
          e.Data.Add("stringInfo", s);
          e.Data["IntInfo"] = i;
          e.Data["DateTimeInfo"] = dt;
          e.HResult = unchecked( (int)0x81234567 );
          throw e;
      }
      catch (Exception e) {
         Console.WriteLine("An exception was thrown.");
         Console.WriteLine(e.Message);
         if (e.Data.Count > 0) {
            Console.WriteLine("  Extra details:");
            foreach (DictionaryEntry de in e.Data)
               Console.WriteLine("    Key: {0,-20}      Value: {1}", 
                                 "'" + de.Key.ToString() + "'", de.Value);
         }
      }
   }
}

Next chapter...

What you will learn in the next chapter:

  1. Get to know Exception.InnerException
  2. Syntax for Exception.InnerException
  3. Example - Exception.InnerException
Home »
  C# Tutorial »
    System »
      Exception
C# Exception Data
C# Exception HelpLink
C# Exception HResult
C# Exception InnerException
C# Exception Message
C# Exception Source
C# Exception StackTrace
C# Exception TargetSite
C# Exception Exception()
C# Exception Exception(String)
C# Exception Exception(SerializationInfo, S...
C# Exception Exception(String, Exception)
C# Exception Equals(Object)
C# Exception Finalize
C# Exception GetBaseException
C# Exception GetHashCode
C# Exception GetObjectData
C# Exception GetType
C# Exception MemberwiseClone
C# Exception ToString