Exception.HResult Exception.HResult Exception.HResult Exception.HResult Property

정의

특정 예외에 할당된 코드화된 숫자 값인 HRESULT를 가져오거나 설정합니다. Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

public:  property int HResult {  public:
int get(); protected:
 void set(int value); };
public int HResult { get; protected set; }
member this.HResult : int with get, set
Public Property HResult As Integer
속성 값

HRESULT 값입니다. The HRESULT value.

예제

다음 코드 예제에서는 파생 된 정의 Exception 설정 하는 클래스는 HResult 속성을 생성자의 사용자 지정 값입니다.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
   {
   private:
      static int SecondLevelHResult = (int)0x81234567;

   public:

      // 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()
   {
      try
      {
         try
         {
            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" );
   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()
*/
// 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.
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()

설명

HRESULT는 32 비트 값, 다른 세 가지 필드로 구분: 심각도 코드, 기능 코드, 및 오류 코드입니다.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. 각 예외는 고유한 HRESULT에 매핑됩니다.Each exception is mapped to a distinct HRESULT. 관리 코드에서 예외를 throw 하는 경우 런타임 HRESULT를 COM 클라이언트에 전달 합니다.When managed code throws an exception, the runtime passes the HRESULT to the COM client. 관리 되지 않는 코드 오류를 반환 하는 경우 다음 런타임에 의해 throw 되는 예외를 HRESULT 변환 됩니다.When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime. HRESULT 값과 해당 해당.NET Framework 예외에 대 한 정보를 참조 하세요 방법: Map HRESULTs and Exceptions합니다.For information about HRESULT values and their corresponding .NET Framework exceptions, see How to: Map HRESULTs and Exceptions. 참조 일반적인 HRESULT 값 발생할 가능성이 가장 높은 값의 목록은 Windows 문서에서입니다.See Common HRESULT Values in the Windows documentation for a list of the values you are most likely to encounter.

부터 합니다 .NET Framework 4.5.NET Framework 4.5, HResult 반면 해당 getter는 공용 속성의 setter 보호 됩니다.Starting with the .NET Framework 4.5.NET Framework 4.5, the HResult property's setter is protected, whereas its getter is public. .NET Framework의 이전 버전에서는 getter 및 setter를 모두 보호 됩니다.In previous versions of the .NET Framework, both getter and setter are protected.

적용 대상

추가 정보