SALES: 1-800-867-1380
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Exception.HResult Property

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

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

protected int HResult { get; set; }
/** @property */
protected int get_HResult ()

/** @property */
protected void set_HResult (int value)

protected function get HResult () : int

protected function set HResult (value : int)

Property Value

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.

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

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

namespace NDP_UE_CS
{
    // Create the derived exception class.
    class SecondLevelException : Exception
    {
        const int SecondLevelHResult = unchecked( (int)0x81234567 );

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

    class HResultDemo 
    {
        public static void Main() 
        {
            Console.WriteLine( 
                "This example of Exception.HResult " +
                "generates the following output.\n" );

            // This function forces a division by 0 and throws 
            // a second exception.
            try
            {
                try
                {
                    int  zero = 0;
                    int  ecks = 1 / zero;
                }
                catch( Exception ex )
                {
                    throw new SecondLevelException( 
                        "Forced a division by 0 and threw " +
                        "a second exception.", ex );
                }
            }
            catch( Exception ex )
            {
                Console.WriteLine( ex.ToString( ) );
            }
        }
    }
}

/*
This example of Exception.HResult generates the following output.

NDP_UE_CS.SecondLevelException: (HRESULT:0x81234567) Forced a division by 0 and
 threw a second exception. ---> System.DivideByZeroException: Attempted to divi
de by zero.
   at NDP_UE_CS.HResultDemo.Main()
   --- End of inner exception stack trace ---
   at NDP_UE_CS.HResultDemo.Main()
*/

// Example for the Exception.HResult property.
package NDP_UE_JSL ; 

import System.* ;

// Create the derived exception class.
class SecondLevelException  extends System.Exception
{
    private int SecondLevelHResult = (int)(0x81234567);

    // Set HResult for this exception, and include it in 
    // the exception message.
    public SecondLevelException(String message, System.Exception inner)
    {
        super(String.Format("(HRESULT:0x{1:X8}) {0}", message, 
            ((System.Int32)(int)(0x81234567)).ToString("X8")), inner);
        set_HResult(SecondLevelHResult);
    } //SecondLevelException
} //SecondLevelException

class HResultDemo
{
    public static void main(String[] args)
    {
        Console.WriteLine(("This example of Exception.HResult " 
            + "generates the following output.\n"));

        // This function forces a division by 0 and throws 
        // a second exception.
        try {
            try {
                int zero = 0;
                int ecks = 1 / zero;
            }
            catch (System.Exception ex) {
                throw new SecondLevelException("Forced a division by 0 and " 
                    + "threw a second exception.", ex);
            }
        }
        catch (System.Exception ex) {
            Console.WriteLine(ex.toString());
        }
    } //main
} //HResultDemo
   
/*
This example of Exception.HResult generates the following output.

NDP_UE_JSL.SecondLevelException: (HRESULT:0x81234567) Forced a division by 0 
and threw a second exception. ---> System.DivideByZeroException: Attempted to 
divi de by zero.
   at NDP_UE_JSL.HResultDemo.Main()
   --- End of inner exception stack trace ---
   at NDP_UE_JSL.HResultDemo.Main()
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft