Exception.HResult Özellik

Tanım

Belirli bir özel duruma atanan kodlanmış sayısal bir değer olan HRESULT değerini alır veya ayarlar.

C#
public int HResult { get; protected set; }
C#
public int HResult { get; set; }
C#
protected int HResult { get; set; }

Özellik Değeri

Int32

HRESULT değeri.

Örnekler

Aşağıdaki kod örneği, özelliğini oluşturucusunda HResult özel bir değere ayarlayan türetilmiş Exception bir sınıfı tanımlar.

C#
// 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()
*/

Açıklamalar

HRESULT üç farklı alana ayrılmış 32 bitlik bir değerdir: önem derecesi kodu, tesis kodu ve hata kodu. Önem derecesi kodu, dönüş değerinin bilgileri, uyarıyı veya hatayı temsil edip etmediğini gösterir. Tesis kodu, hatadan sorumlu sistemin alanını tanımlar. Hata kodu, özel durumu temsil etmek üzere atanan benzersiz bir sayıdır. Her özel durum ayrı bir HRESULT ile eşlenir. Yönetilen kod bir özel durum oluştururken çalışma zamanı HRESULT'u COM istemcisine geçirir. Yönetilmeyen kod bir hata döndürdüğünde HRESULT bir özel duruma dönüştürülür ve bu durum çalışma zamanı tarafından oluşturulur. HRESULT değerleri ve karşılık gelen .NET Framework özel durumları hakkında bilgi için bkz. Nasıl yapılır: HRESULTs ve Özel Durumları Eşleme. Karşılaşabileceğiniz değerlerin listesi için Windows belgelerindeki Ortak HRESULT Değerleri'ne bakın.

.NET Framework 4.5'den başlayarak özelliğin HResult ayarlayıcısı korunurken, getter geneldir. .NET Framework önceki sürümlerinde hem getter hem de setter korunur.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Ayrıca bkz.