다음을 통해 공유


Exception.HResult 속성

정의

특정 예외에 할당된 코드화된 숫자 값인 HRESULT를 가져오거나 설정합니다.

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

속성 값

Int32

HRESULT 값입니다.

예제

다음 코드 예제에서는 해당 생성자의 사용자 지정 값으로 속성을 설정 HResult 하는 파생된 Exception 클래스를 정의 합니다.

// 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()
{
   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()
        {
            // 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.
open System

let secondLevelHResult = 0x81234567

// Create the derived exception class.
// Set HResult for this exception, and include it in the exception message.
type SecondLevelException(message, inner) as this =
    inherit Exception($"(HRESULT:0x{secondLevelHResult:X8}) %s{message}", inner)
    do
        this.HResult <- secondLevelHResult

// The following forces a division by 0 and throws a second exception.
try
    try
        let zero = 0
        let ecks = 1 / zero
        ()
    with ex ->
        raise (SecondLevelException("Forced a division by 0 and threw a second exception.", ex) )
with ex ->
    printfn $"{ex}"

// This example of Exception.HResult generates the following output.
//
// NDP_UE_FS+SecondLevelException: (HRESULT:0x81234567) Forced a division by 0 and
//  threw a second exception. ---> System.DivideByZeroException: Attempted to divi
// de by zero.
//    at <StartupCode$fs>.$NDP_UE_FS.main@()
//    --- End of inner exception stack trace ---
//    at <StartupCode$fs>.$NDP_UE_FS.main@()
' Example for the Exception.HResult property.
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
    End Class

    Module HResultDemo
       
        Sub Main()
            ' 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

    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에 매핑됩니다. 관리 코드가 예외를 throw하면 런타임은 HRESULT를 COM 클라이언트에 전달합니다. 관리되지 않는 코드가 오류를 반환하면 HRESULT가 예외로 변환된 다음 런타임에 의해 throw됩니다. HRESULT 값 및 해당 .NET Framework 예외에 대한 자세한 내용은 방법: HRESULT 및 예외 매핑을 참조하세요. 발생할 가능성이 가장 높은 값 목록은 Windows 설명서의 공통 HRESULT 값을 참조하세요.

.NET Framework 4.5 HResult 부터 속성의 setter는 보호되지만 getter는 공용입니다. 이전 버전의 .NET Framework getter와 setter가 모두 보호됩니다.

적용 대상

추가 정보