Exception.HResult Proprietà

Definizione

Ottiene o imposta HRESULT, un valore numerico codificato che viene assegnato a un'eccezione specifica.

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

Valore della proprietà

Valore HRESULT.

Esempio

Nell'esempio di codice seguente viene definita una classe derivata Exception che imposta la HResult proprietà su un valore personalizzato nel relativo costruttore.

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

Commenti

HRESULT è un valore a 32 bit, suddiviso in tre campi differenti: un codice di gravità, un codice di funzionalità e un codice di errore. Il codice di gravità indica se il valore restituito rappresenta informazioni, avvisi o errori. Il codice di funzionalità identifica l'area del sistema responsabile dell'errore. Il codice di errore è un numero univoco assegnato per rappresentare l'eccezione. Ogni eccezione viene mappata a un HRESULT distinto. Quando il codice gestito genera un'eccezione, il runtime passa HRESULT al client COM. Quando il codice non gestito restituisce un errore, HRESULT viene convertito in un'eccezione, generata dal runtime. Per informazioni sui valori HRESULT e sulle relative eccezioni di .NET Framework corrispondenti, vedere Procedura: Mapping di HRESULTs e eccezioni. Vedere Valori HRESULT comuni nella documentazione di Windows per un elenco dei valori che è più probabile riscontrare.

A partire da .NET Framework 4.5, il setter della proprietà è protetto, mentre il HResult relativo getter è pubblico. Nelle versioni precedenti di .NET Framework, sia getter che setter sono protetti.

Si applica a

Prodotto Versioni
.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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Vedi anche