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.

protected:
 property int HResult { int get(); void set(int value); };
protected int HResult { get; set; }
member this.HResult : int with get, set
Protected 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.
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()
            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

    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. 當 managed 程式碼擲回例外狀況時,執行時間會將 HRESULT 傳遞給 COM 用戶端。When managed code throws an exception, the runtime passes the HRESULT to the COM client. 當非受控程式碼傳回錯誤時,HRESULT 會轉換成例外狀況,然後由執行時間擲回。When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime. 如需 HRESULT 值及其對應 .NET Framework 例外狀況的詳細資訊,請參閱如何:對應 Hresult 和例外狀況。For information about HRESULT values and their corresponding .NET Framework exceptions, see How to: Map HRESULTs and Exceptions. 如需最有可能遇到的值清單,請參閱 Windows 檔中的通用 HRESULT 值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 ,屬性的 setter 會受到保護,而其 getter 則是公用的。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.

適用於

另請參閱