FirstChanceExceptionEventArgs Classe

Definizione

Fornisce dati per l'evento di notifica generato quando si verifica per la prima volta un'eccezione gestita, prima che Common Language Runtime inizi la ricerca di gestori di eventi.

C#
public class FirstChanceExceptionEventArgs : EventArgs
Ereditarietà
FirstChanceExceptionEventArgs

Esempio

Nell'esempio seguente viene creata una serie di domini applicazione denominati Child_0 tramite Child_3, con un Worker oggetto in ogni dominio applicazione. Ogni Worker oggetto ha un riferimento all'oggetto Worker nel dominio applicazione successivo, ad eccezione di nell'ultimo Worker dominio applicazione. L'evento FirstChanceException viene gestito in tutti i domini applicazione ad eccezione Child_1di .

Dopo aver creato i domini applicazione, il dominio applicazione predefinito chiama il TestException metodo per il primo dominio applicazione figlio. Ogni Worker oggetto chiama il TestException metodo per il successivo, finché l'ultima Worker genera un'eccezione gestita o non gestita. Di conseguenza, il thread corrente passa attraverso tutti i domini applicazione e TestException viene aggiunto allo stack in ogni dominio applicazione.

Quando l'ultimo Worker oggetto gestisce l'eccezione, l'evento FirstChanceException viene generato solo nell'ultimo dominio applicazione. Gli altri domini applicazione non hanno mai la possibilità di gestire l'eccezione, quindi l'evento non viene generato.

Quando l'ultimo Worker oggetto non gestisce l'eccezione, l'evento FirstChanceException viene generato in ogni dominio applicazione con un gestore eventi. Al termine di ogni gestore eventi, lo stack continua a eseguire la rimozione fino a quando l'eccezione non viene intercettata dal dominio applicazione predefinito.

Nota

Per verificare l'aumento della visualizzazione dello stack man mano che l'evento viene generato più vicino al dominio applicazione predefinito, passare e.Exception.Message a e.Exception nei FirstChanceHandler gestori eventi. Si noti che quando TestException viene chiamato attraverso i limiti del dominio dell'applicazione, viene visualizzato due volte: una per il proxy e una volta per lo stub.

C#
using System;
using System.Reflection;
using System.Runtime.ExceptionServices;

class FirstChanceExceptionSnippet
{
    static void Main()
    {
        AppDomain.CurrentDomain.FirstChanceException += FirstChanceHandler;

        // Create a set of application domains, with a Worker object in each one.
        // Each Worker object creates the next application domain.
        AppDomain ad = AppDomain.CreateDomain("AD0");
        Worker w = (Worker) ad.CreateInstanceAndUnwrap(
                                typeof(Worker).Assembly.FullName, "Worker");
        w.Initialize(0, 3);

        Console.WriteLine("\r\nThe last application domain throws an exception and catches it:");
        Console.WriteLine();
        w.TestException(true);

        try
        {
            Console.WriteLine(
                "\r\nThe last application domain throws an exception and does not catch it:");
            Console.WriteLine();
            w.TestException(false);
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine("ArgumentException caught in {0}: {1}",
                AppDomain.CurrentDomain.FriendlyName, ex.Message);
        }
    }

    static void FirstChanceHandler(object source, FirstChanceExceptionEventArgs e)
    {
        Console.WriteLine("FirstChanceException event raised in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
    }
}

public class Worker : MarshalByRefObject
{
    private AppDomain ad = null;
    private Worker w = null;

    public void Initialize(int count, int max)
    {
        // Handle the FirstChanceException event in all application domains except
        // AD1.
        if (count != 1)
        {
            AppDomain.CurrentDomain.FirstChanceException += FirstChanceHandler;
        }

        // Create another application domain, until the maximum is reached.
        // Field w remains null in the last application domain, as a signal
        // to TestException().
        if (count < max)
        {
            int next = count + 1;
            ad = AppDomain.CreateDomain("AD" + next);
            w = (Worker) ad.CreateInstanceAndUnwrap(
                             typeof(Worker).Assembly.FullName, "Worker");
            w.Initialize(next, max);
        }
    }

    public void TestException(bool handled)
    {
        // As long as there is another application domain, call TestException() on
        // its Worker object. When the last application domain is reached, throw a
        // handled or unhandled exception.
        if (w != null)
        {
            w.TestException(handled);
        }
        else if (handled)
        {
            try
            {
                throw new ArgumentException("Thrown in " + AppDomain.CurrentDomain.FriendlyName);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("ArgumentException caught in {0}: {1}",
                    AppDomain.CurrentDomain.FriendlyName, ex.Message);
            }
        }
        else
        {
            throw new ArgumentException("Thrown in " + AppDomain.CurrentDomain.FriendlyName);
        }
    }

    static void FirstChanceHandler(object source, FirstChanceExceptionEventArgs e)
    {
        Console.WriteLine("FirstChanceException event raised in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
    }
}

/* This example produces output similar to the following:

The last application domain throws an exception and catches it:

FirstChanceException event raised in AD3: Thrown in AD3
ArgumentException caught in AD3: Thrown in AD3

The last application domain throws an exception and does not catch it:

FirstChanceException event raised in AD3: Thrown in AD3
FirstChanceException event raised in AD2: Thrown in AD3
FirstChanceException event raised in AD0: Thrown in AD3
FirstChanceException event raised in Example.exe: Thrown in AD3
ArgumentException caught in Example.exe: Thrown in AD3
 */

Commenti

Questa classe fornisce il gestore eccezioni per l'evento AppDomain.FirstChanceException con accesso all'eccezione.

Costruttori

FirstChanceExceptionEventArgs(Exception)

Inizializza una nuova istanza della classe FirstChanceExceptionEventArgs con un’eccezione specificata.

Proprietà

Exception

Oggetto eccezione gestita che corrisponde all'eccezione generata nel codice gestito.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Prodotto Versioni
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 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 2.0, 2.1

Vedi anche