FirstChanceExceptionEventArgs 클래스

정의

관리되는 예외가 처음으로 발생할 때 공용 언어 런타임에서 이벤트 처리기를 검색하기 전에 발생하는 알림 이벤트에 대한 데이터를 제공합니다.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
상속
FirstChanceExceptionEventArgs

예제

다음 예제에서는 일련의 명명 된 애플리케이션 도메인을 만듭니다 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 처리 되거나 처리 되지 않은 예외를 throw 합니다.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.ExceptionFirstChanceHandler 이벤트 처리기입니다.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. TestException 라고 애플리케이션 도메인 경계를 가로질러 두 번 나타납니다: 프록시에 한 번씩 및 스텁에 대 한 한 번입니다.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 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the FirstChanceExceptionEventArgs class with a specified exception.

속성

Exception

관리되는 코드에서 throw된 예외에 해당하는 관리되는 예외 개체입니다.The managed exception object that corresponds to the exception thrown in managed code.

메서드

Equals(Object)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.Determines whether the specified object is equal to the current object.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.Serves as the default hash function.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.Returns a string that represents the current object.

(다음에서 상속됨 Object)

적용 대상

추가 정보