Exception::HResult Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

property int HResult {
	int get();
	protected: void set(int value);

Property Value

Type: System::Int32

The HRESULT value.

HRESULT is a 32-bit value, divided into three different fields: a severity code, a facility code, and an error code. The severity code indicates whether the return value represents information, warning, or error. The facility code identifies the area of the system responsible for the error. The error code is a unique number that is assigned to represent the exception. Each exception is mapped to a distinct HRESULT. When managed code throws an exception, the runtime passes the HRESULT to the COM client. When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime. For information about HRESULT values and their corresponding .NET Framework exceptions, see How to: Map HRESULTs and Exceptions. See Common HRESULT Values in the Windows documentation for a list of the values you are most likely to encounter.

Starting with the .NET Framework 4.5, the HResult property's setter is protected, whereas its getter is public. In previous versions of the .NET Framework, both getter and setter are protected.

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

// Example for the Exception::HResult property.
using namespace System;

namespace NDP_UE_CPP

   // Create the derived exception class.
   ref class SecondLevelException: public Exception
      static int SecondLevelHResult = (int)0x81234567;


      // Set HResult for this exception, and include it in 
      // the exception message.
      SecondLevelException( String^ message, Exception^ inner )
         : Exception( String::Format( "(HRESULT:0x{1:X8}) {0}", message, SecondLevelHResult ), inner )
         HResult = SecondLevelHResult;


   // This function forces a division by 0 and throws 
   // a second exception.
   void DivideBy0()
            int zero = 0;
            int ecks = 1 / zero;
         catch ( Exception^ ex ) 
            throw gcnew SecondLevelException( "Forced a division by 0 and threw "
            "a second exception.",ex );

      catch ( Exception^ ex ) 
         Console::WriteLine( ex->ToString() );



int main()
   Console::WriteLine( "This example of Exception::HResult "
   "generates the following output.\n" );

This example of Exception::HResult generates the following output.

NDP_UE_CPP.SecondLevelException: (HRESULT:0x81234567) Forced a division by 0 an
d threw a second exception. ---> System.DivideByZeroException: Attempted to div
ide by zero.
   at NDP_UE_CPP.DivideBy0()
   --- End of inner exception stack trace ---
   at NDP_UE_CPP.DivideBy0()

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top