FirstChanceExceptionEventArgs FirstChanceExceptionEventArgs FirstChanceExceptionEventArgs FirstChanceExceptionEventArgs Class

定義

マネージド例外が最初に発生したときに発生する通知イベントのデータを、共通言語ランタイムによるイベント ハンドラーの検索が開始される前に提供します。Provides data for the notification event that is raised when a managed exception first occurs, before the common language runtime begins searching for event handlers.

public ref class FirstChanceExceptionEventArgs : EventArgs
public class FirstChanceExceptionEventArgs : EventArgs
type FirstChanceExceptionEventArgs = class
    inherit EventArgs
Public Class FirstChanceExceptionEventArgs
Inherits EventArgs
継承
FirstChanceExceptionEventArgsFirstChanceExceptionEventArgsFirstChanceExceptionEventArgsFirstChanceExceptionEventArgs

次の例は、一連のという名前のアプリケーション ドメインを作成Child_0を通じてChild_3で、Worker各アプリケーション ドメイン内のオブジェクト。The following example creates a series of application domains named Child_0 through Child_3, with a Worker object in each application domain. Workerオブジェクトへの参照には、Worker以外の次のアプリケーション ドメイン内のオブジェクト、Worker最後のアプリケーション ドメインでします。Each Worker object has a reference to the Worker object in the next application domain, except for the Worker in the last application domain. FirstChanceExceptionを除くすべてのアプリケーション ドメインでイベントを処理Child_1します。The FirstChanceException event is handled in all application domains except Child_1.

アプリケーション ドメインが作成されると、既定のアプリケーション ドメインが呼び出し、TestExceptionメソッドの最初の子アプリケーション ドメイン。When the application domains have been created, the default application domain calls the TestException method for the first child application domain. Workerオブジェクトの呼び出し、TestException最後まで、次のメソッドWorkerが処理または未処理の例外をスローします。Each Worker object calls the TestException method for the next, until the last Worker throws an exception that is either handled or unhandled. 現在のスレッドがすべてのアプリケーション ドメインを通過するため、およびTestExceptionアプリケーション ドメインごとにスタックに追加されます。Thus, the current thread passes through all the application domains, and TestException is added to the stack in each application domain.

ときに、最終Workerオブジェクト、例外を処理する、FirstChanceException最後のアプリケーション ドメインでのみイベントが発生します。When the last Worker object handles the exception, the FirstChanceException event is raised only in the last application domain. 他のアプリケーション ドメインでは、イベントは発生しませんので、例外を処理するためにされなくなります。The other application domains never get a chance to handle the exception, so the event is not raised.

ときに、最後Workerオブジェクトが例外を処理していない、FirstChanceExceptionイベント ハンドラーを持っている各アプリケーション ドメインでイベントが発生します。When the last Worker object does not handle the exception, the FirstChanceException event is raised in each application domain that has an event handler. 各イベント ハンドラーが完了すると、スタックがアンワインドの既定のアプリケーション ドメインで例外がキャッチされるまで続行されます。After each event handler has finished, the stack continues to unwind until the exception is caught by the default application domain.

注意

イベントは近くの履歴の表示の拡大と既定のアプリケーション ドメインに近づけるためのを表示する変更e.Exception.Messagee.Exceptionで、FirstChanceHandlerイベント ハンドラー。To see how the stack display grows as the event is raised closer and closer to the default application domain, change e.Exception.Message to e.Exception in the FirstChanceHandler event handlers. そのときに注意してくださいTestException2 回表示されるアプリケーション ドメインの境界を越えて呼び出される: プロキシでスタブに 1 回です。Notice that when TestException is called across application domain boundaries, it appears twice: once for the proxy and once for the stub.

using System;
using System.Reflection;
using System.Runtime.ExceptionServices;

class Example
{
    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
 */
Imports System.Reflection
Imports System.Runtime.ExceptionServices

Class Example

    Shared Sub Main()
    
        AddHandler AppDomain.CurrentDomain.FirstChanceException, AddressOf FirstChanceHandler

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

        Console.WriteLine(vbCrLf & "The last application domain throws an exception and catches it:")
        Console.WriteLine()
        w.TestException(true)

        Try
            Console.WriteLine(vbCrLf & 
                "The last application domain throws an exception and does not catch it:")
            Console.WriteLine()
            w.TestException(false) 

        Catch ex As ArgumentException
        
            Console.WriteLine("ArgumentException caught in {0}: {1}", 
                AppDomain.CurrentDomain.FriendlyName, ex.Message)
        End Try
    End Sub

    Shared Sub FirstChanceHandler(ByVal source As Object, 
                                  ByVal e As FirstChanceExceptionEventArgs)
    
        Console.WriteLine("FirstChanceException event raised in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, e.Exception.Message)
    End Sub
End Class

Public Class Worker
    Inherits MarshalByRefObject

    Private ad As AppDomain = Nothing
    Private w As Worker = Nothing

    Public Sub Initialize(ByVal count As Integer, ByVal max As Integer)
    
        ' Handle the FirstChanceException event in all application domains except
        ' AD1.
        If count <> 1
        
            AddHandler AppDomain.CurrentDomain.FirstChanceException, AddressOf FirstChanceHandler

        End If

        ' Create another application domain, until the maximum is reached.
        ' Field w remains Nothing in the last application domain, as a signal 
        ' to TestException(). 
        If count < max
            Dim nextAD As Integer = count + 1
            ad = AppDomain.CreateDomain("AD" & nextAD)
            w = CType(ad.CreateInstanceAndUnwrap(
                      GetType(Worker).Assembly.FullName, "Worker"),
                      Worker)
            w.Initialize(nextAD, max)
        End If
    End Sub

    Public Sub TestException(ByVal handled As Boolean)
    
        ' 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 IsNot Nothing
        
            w.TestException(handled)

        Else If handled
        
            Try
                Throw New ArgumentException("Thrown in " & AppDomain.CurrentDomain.FriendlyName)

            Catch ex As ArgumentException
            
                Console.WriteLine("ArgumentException caught in {0}: {1}", 
                    AppDomain.CurrentDomain.FriendlyName, ex.Message)
            End Try
        Else
        
            Throw New ArgumentException("Thrown in " & AppDomain.CurrentDomain.FriendlyName)
        End If
    End Sub

    Shared Sub FirstChanceHandler(ByVal source As Object, 
                                  ByVal e As FirstChanceExceptionEventArgs)
    
        Console.WriteLine("FirstChanceException event raised in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, e.Exception.Message)
    End Sub
End Class

' 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

注釈

このクラスの例外ハンドラーを提供する、AppDomain.FirstChanceException例外へのアクセス権を持つイベント。This class provides the exception handler for the AppDomain.FirstChanceException event with access to the exception.

コンストラクター

FirstChanceExceptionEventArgs(Exception) FirstChanceExceptionEventArgs(Exception) FirstChanceExceptionEventArgs(Exception) FirstChanceExceptionEventArgs(Exception)

指定した例外を使用して、FirstChanceExceptionEventArgs クラスの新しいインスタンスを初期化します。Initializes a new instance of the FirstChanceExceptionEventArgs class with a specified exception.

プロパティ

Exception Exception Exception Exception

マネージド コードによってスローされる例外に対応するマネージド例外オブジェクト。The managed exception object that corresponds to the exception thrown in managed code.

メソッド

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

適用対象

こちらもご覧ください