다음을 통해 공유


Exception.HResult 속성

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

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Protected Property HResult As Integer
‘사용 방법
Dim value As Integer

value = Me.HResult

Me.HResult = value
protected int HResult { get; set; }
protected:
property int HResult {
    int get ();
    void set (int value);
}
/** @property */
protected int get_HResult ()

/** @property */
protected void set_HResult (int value)
protected function get HResult () : int

protected function set HResult (value : int)

속성 값

HRESULT 값입니다.

설명

HRESULT는 32 비트 값이며 심각도 코드, 기능 코드, 오류 코드의 세 가지 다른 필드로 나누어집니다. 심각도 코드는 반환 값이 정보, 경고 또는 오류를 표시하는지 여부를 나타냅니다. 기능 코드는 오류를 담당하는 시스템의 영역을 식별합니다. 오류 코드는 예외를 표시하기 위해 할당되는 고유한 숫자입니다. 각각의 예외는 고유한 HRESULT에 매핑됩니다. 관리 코드가 예외를 throw할 때, 공용 언어 런타임은 HRESULT를 COM 클라이언트에 전달합니다. 비관리 코드가 오류를 반환하면, HRESULT는 예외로 전환되고 공용 언어 런타임에 의해 throw됩니다.

예제

다음 코드 예제에서는 HResult 속성을 생성자에 설정하는 파생 Exception 클래스를 정의합니다.

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

Exception 클래스
Exception 멤버
System 네임스페이스