방법: 첫째 예외 알림 받기

AppDomain 클래스의 FirstChanceException 이벤트를 사용하면 공용 언어 런타임이 예외 처리기 검색을 시작하기 전에 예외가 throw되었다는 알림을 받을 수 있습니다.

이 이벤트는 애플리케이션 도메인 수준에서 발생합니다. 하나의 실행 스레드가 여러 애플리케이션 도메인을 통과할 수 있으므로 한 애플리케이션 도메인에서 처리되지 않은 예외가 다른 애플리케이션 도메인에서 처리될 수 있습니다. 애플리케이션 도메인이 예외를 처리할 때까지 이벤트 처리기를 추가한 각 애플리케이션 도메인에서 알림이 발생합니다.

이 문서의 절차 및 예제에서는 애플리케이션 도메인 하나가 있는 간단한 프로그램과 직접 만든 애플리케이션 도메인에서 첫째 예외 알림을 받는 방법을 보여 줍니다.

여러 애플리케이션 도메인에 걸쳐 있는 좀 더 복잡한 예제의 경우 FirstChanceException 이벤트에 대한 예제를 참조하세요.

기본 애플리케이션 도메인에서 첫째 예외 알림 받기

다음 절차에서는 애플리케이션에 대한 진입점인 Main() 메서드는 기본 애플리케이션 도메인에서 실행됩니다.

기본 애플리케이션 도메인에서 첫째 예외 알림을 보여 주려면

  1. 람다 함수를 사용하여 FirstChanceException 이벤트에 대한 이벤트 처리기를 정의하고 이벤트에 연결합니다. 이 예제에서 이벤트 처리기는 이벤트가 처리된 애플리케이션 도메인의 이름 및 해당 예외의 Message 속성을 출력합니다.

    using System;
    using System.Runtime.ExceptionServices;
    
    class Example
    {
        static void Main()
        {
            AppDomain.CurrentDomain.FirstChanceException +=
                (object source, FirstChanceExceptionEventArgs e) =>
                {
                    Console.WriteLine("FirstChanceException event raised in {0}: {1}",
                        AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
                };
    
    Imports System.Runtime.ExceptionServices
    
    Class Example
    
        Shared Sub Main()
    
            AddHandler AppDomain.CurrentDomain.FirstChanceException,
                       Sub(source As Object, e As FirstChanceExceptionEventArgs)
                           Console.WriteLine("FirstChanceException event raised in {0}: {1}",
                                             AppDomain.CurrentDomain.FriendlyName,
                                             e.Exception.Message)
                       End Sub
    
  2. 예외를 throw 및 catch합니다. 런타임에서 예외 처리기를 찾기 전에 FirstChanceException 이벤트가 발생하고 메시지를 표시합니다. 이 메시지 다음에는 예외 처리기에 의해 표시되는 메시지가 옵니다.

    try
    {
        throw new ArgumentException("Thrown in " + AppDomain.CurrentDomain.FriendlyName);
    }
    catch (ArgumentException ex)
    {
        Console.WriteLine("ArgumentException caught in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, ex.Message);
    }
    
    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
    
  3. 예외를 throw하지만 catch하지 않습니다. 런타임에서 예외 처리기를 찾기 전에 FirstChanceException 이벤트가 발생하고 메시지를 표시합니다. 예외 처리기가 없으므로 애플리케이션이 종료됩니다.

            throw new ArgumentException("Thrown in " + AppDomain.CurrentDomain.FriendlyName);
        }
    }
    
            Throw New ArgumentException("Thrown in " & AppDomain.CurrentDomain.FriendlyName)
        End Sub
    End Class
    

    이 절차의 처음 세 단계에 표시된 코드는 완전한 콘솔 애플리케이션을 구성합니다. 애플리케이션의 출력은 .exe 파일의 이름에 따라 달라집니다. 기본 애플리케이션 도메인의 이름이 .exe 파일의 이름 및 확장명으로 구성되기 때문입니다. 샘플 출력은 다음을 참조하세요.

    /* This example produces output similar to the following:
    
    FirstChanceException event raised in Example.exe: Thrown in Example.exe
    ArgumentException caught in Example.exe: Thrown in Example.exe
    FirstChanceException event raised in Example.exe: Thrown in Example.exe
    
    Unhandled Exception: System.ArgumentException: Thrown in Example.exe
       at Example.Main()
     */
    
    ' This example produces output similar to the following:
    '
    'FirstChanceException event raised in Example.exe: Thrown in Example.exe
    'ArgumentException caught in Example.exe: Thrown in Example.exe
    'FirstChanceException event raised in Example.exe: Thrown in Example.exe
    '
    'Unhandled Exception: System.ArgumentException: Thrown in Example.exe
    '   at Example.Main()
    

다른 애플리케이션 도메인에서 첫째 예외 알림 받기

프로그램이 둘 이상의 애플리케이션 도메인을 포함하는 경우 알림을 받는 애플리케이션 도메인을 선택할 수 있습니다.

직접 만든 애플리케이션 도메인에서 첫째 예외 알림을 받으려면

  1. FirstChanceException 이벤트에 대한 이벤트 처리기를 정의합니다. 이 예제에서는 이벤트가 처리된 애플리케이션 도메인의 이름 및 해당 예외의 static 속성을 출력하는 Shared 메서드(Visual Basic에서는 Message 메서드)를 사용합니다.

    static void FirstChanceHandler(object source, FirstChanceExceptionEventArgs e)
    {
        Console.WriteLine("FirstChanceException event raised in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
    }
    
    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
    
  2. 애플리케이션 도메인을 만들고 해당 애플리케이션 도메인에 대한 FirstChanceException 이벤트에 이벤트 처리기를 추가합니다. 이 예제에서 애플리케이션 도메인의 이름은 AD1입니다.

    AppDomain ad = AppDomain.CreateDomain("AD1");
    ad.FirstChanceException += FirstChanceHandler;
    
    Dim ad As AppDomain = AppDomain.CreateDomain("AD1")
    AddHandler ad.FirstChanceException, AddressOf FirstChanceHandler
    

    동일한 방식으로 기본 애플리케이션 도메인에서 이 이벤트를 처리할 수 있습니다. Main()static(Visual Basic에서는 Shared) AppDomain.CurrentDomain 속성을 사용하여 기본 애플리케이션 도메인에 대한 참조를 가져옵니다.

애플리케이션 도메인에서 첫째 예외 알림을 보여 주려면

  1. 이전 절차에서 만든 애플리케이션 도메인에 Worker 개체를 만듭니다. 이 문서의 끝에 있는 전체 예제와 같이 Worker 클래스는 public이어야 하고 MarshalByRefObject에서 파생되어야 합니다.

    Worker w = (Worker) ad.CreateInstanceAndUnwrap(
                            typeof(Worker).Assembly.FullName, "Worker");
    
    Dim w As Worker = CType(ad.CreateInstanceAndUnwrap(
                                GetType(Worker).Assembly.FullName, "Worker"),
                            Worker)
    
  2. 예외를 throw하는 Worker 개체의 메서드를 호출합니다. 이 예제에서는 Thrower 메서드가 두 번 호출됩니다. 첫 번째 호출에서는 메서드 인수가 true이므로 메서드가 자체 예외를 catch합니다. 두 번째 호출에서는 인수가 false이며, Main() 메서드가 기본 애플리케이션 도메인에서 예외를 catch합니다.

    // The worker throws an exception and catches it.
    w.Thrower(true);
    
    try
    {
        // The worker throws an exception and doesn't catch it.
        w.Thrower(false);
    }
    catch (ArgumentException ex)
    {
        Console.WriteLine("ArgumentException caught in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, ex.Message);
    }
    
    ' The worker throws an exception and catches it.
    w.Thrower(true)
    
    Try
        ' The worker throws an exception and doesn't catch it.
        w.Thrower(false)
    
    Catch ex As ArgumentException
    
        Console.WriteLine("ArgumentException caught in {0}: {1}",
            AppDomain.CurrentDomain.FriendlyName, ex.Message)
    End Try
    
  3. Thrower 메서드에 코드를 배치하여 메서드가 자체 예외를 처리하는지 여부를 제어합니다.

    if (catchException)
    {
        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);
    }
    
    If catchException
    
        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
    

예시

다음 예제에서는 AD1이라는 애플리케이션 도메인을 만들고 애플리케이션 도메인의 FirstChanceException 이벤트에 이벤트 처리기를 추가합니다. 예제에서는 애플리케이션 도메인에서 Worker 클래스 인스턴스를 만들고 ArgumentException을 throw하는 Thrower 메서드를 호출합니다. 인수 값에 따라 메서드가 예외를 catch하거나 예외를 처리하지 못합니다.

Thrower 메서드가 AD1에서 예외를 throw할 때마다 AD1에서 FirstChanceException 이벤트가 발생하고 이벤트 처리기가 메시지를 표시합니다. 그러면 런타임이 예외 처리기를 찾습니다. 첫 번째 경우에는 예외 처리기가 AD1에 있습니다. 두 번째 경우에는 예외가 AD1에서 처리되지 않고 기본 애플리케이션 도메인에서 catch됩니다.

참고 항목

기본 애플리케이션 도메인의 이름은 실행 파일의 이름과 같습니다.

기본 애플리케이션 도메인에 FirstChanceException 이벤트 처리기를 추가하면 기본 애플리케이션 도메인이 예외를 처리하기 전에 이벤트가 발생하고 처리됩니다. 이를 확인하려면 Main()의 시작 부분에 C# 코드 AppDomain.CurrentDomain.FirstChanceException += FirstChanceException;(Visual Basic에서는 AddHandler AppDomain.CurrentDomain.FirstChanceException, FirstChanceException)을 추가합니다.

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

class Example
{
    static void Main()
    {
        // To receive first chance notifications of exceptions in
        // an application domain, handle the FirstChanceException
        // event in that application domain.
        AppDomain ad = AppDomain.CreateDomain("AD1");
        ad.FirstChanceException += FirstChanceHandler;

        // Create a worker object in the application domain.
        Worker w = (Worker) ad.CreateInstanceAndUnwrap(
                                typeof(Worker).Assembly.FullName, "Worker");

        // The worker throws an exception and catches it.
        w.Thrower(true);

        try
        {
            // The worker throws an exception and doesn't catch it.
            w.Thrower(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
{
    public void Thrower(bool catchException)
    {
        if (catchException)
        {
            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);
        }
    }
}

/* This example produces output similar to the following:

FirstChanceException event raised in AD1: Thrown in AD1
ArgumentException caught in AD1: Thrown in AD1
FirstChanceException event raised in AD1: Thrown in AD1
ArgumentException caught in Example.exe: Thrown in AD1
 */
Imports System.Reflection
Imports System.Runtime.ExceptionServices

Class Example
    Shared Sub Main()

        ' To receive first chance notifications of exceptions in 
        ' an application domain, handle the FirstChanceException
        ' event in that application domain.
        Dim ad As AppDomain = AppDomain.CreateDomain("AD1")
        AddHandler ad.FirstChanceException, AddressOf FirstChanceHandler


        ' Create a worker object in the application domain.
        Dim w As Worker = CType(ad.CreateInstanceAndUnwrap(
                                    GetType(Worker).Assembly.FullName, "Worker"),
                                Worker)

        ' The worker throws an exception and catches it.
        w.Thrower(true)

        Try
            ' The worker throws an exception and doesn't catch it.
            w.Thrower(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

    Public Sub Thrower(ByVal catchException As Boolean)

        If catchException

            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
End Class

' This example produces output similar to the following:
'
'FirstChanceException event raised in AD1: Thrown in AD1
'ArgumentException caught in AD1: Thrown in AD1
'FirstChanceException event raised in AD1: Thrown in AD1
'ArgumentException caught in Example.exe: Thrown in AD1

참고 항목