This documentation is archived and is not being maintained.

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)

'Declaration
Protected Property HResult As Integer
'Usage
Dim value As Integer 

value = Me.HResult 

Me.HResult = 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.

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

' 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()

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: