Export (0) Print
Expand All
5 out of 6 rated this helpful - Rate this topic

Exception.HResult Property

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

[Visual Basic]
Protected Property HResult As Integer
[C#]
protected int HResult {get; set;}
[C++]
protected: __property int get_HResult();
protected: __property void set_HResult(int);
[JScript]
protected function get HResult() : int;
protected function set HResult(int);

Property Value

The HRESULT value.

Remarks

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.

Example

[Visual Basic, C#, C++] The following code example defines a derived Exception class that sets the HResult property in its constructor.

[Visual Basic] 
' Example for the Exception.HResult property.
Imports System
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' Create the derived exception class.
    Class SecondLevelException
        Inherits Exception

        Private Const SecondLevelHResult As Integer = &H81234567
       
        ' Set HResult for this exception, and include it in 
        ' the exception message.
        Public Sub New(message As String, inner As Exception)

            MyBase.New( String.Format( "(HRESULT:0x{1:X8}) {0}", _
                message, SecondLevelHResult ), inner )
            HResult = SecondLevelHResult
        End Sub ' New
    End Class ' SecondLevelException

    Module HResultDemo
       
        Sub Main()
            Console.WriteLine( _
                "This example of Exception.HResult " & _
                "generates the following output." & vbCrLf )
              
            ' This function forces a division by 0 and throws 
            ' a second exception.
            Try
                Try
                    Dim zero As Integer = 0
                    Dim ecks As Integer = 1 \ zero

                Catch ex As Exception
                    Throw New SecondLevelException( _
                        "Forced a division by 0 and threw " & _
                        "a second exception.", ex )
                End Try
              
            Catch ex As Exception
                Console.WriteLine( ex.ToString( ) )
            End Try
        End Sub ' Main

    End Module ' HResultDemo
End Namespace ' NDP_UE_VB

' This example of Exception.HResult generates the following output.
' 
' NDP_UE_VB.SecondLevelException: (HRESULT:0x81234567) Forced a division by 0 a
' nd threw a second exception. ---> System.DivideByZeroException: Attempted to
' divide by zero.
'    at NDP_UE_VB.HResultDemo.Main()
'    --- End of inner exception stack trace ---
'    at NDP_UE_VB.HResultDemo.Main()

[C#] 
// 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()
*/

[C++] 
// Example for the Exception::HResult property.
#using <mscorlib.dll>
using namespace System;

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

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

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

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

    NDP_UE_CPP::DivideBy0( );
}

/*
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()
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Exception Class | Exception Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.