반응형
using System;
using System.Windows;
namespace YourNamespace
{
public static class GlobalErrorHandler
{
public static void Initialize()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.Current.DispatcherUnhandledException += App_DispatcherUnhandledException;
}
private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "오류 발생", MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception ex)
{
MessageBox.Show(ex.Message, "오류 발생", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
3. 사용 방법
WPF 애플리케이션의 App.xaml.cs 파일에서 GlobalErrorHandler.Initialize()를 호출하여 전역 예외 처리기를 설정합니다.
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
GlobalErrorHandler.Initialize(); // 전역 오류 처리기 초기화
}
반응형
'프로그래밍 _공부자료. > C#' 카테고리의 다른 글
C# .xml 파일 실행파일 경로 가져오는법 (0) | 2024.07.31 |
---|---|
C# 로그 클래스 (0) | 2024.07.23 |
C# ATM 기능 만들기 (0) | 2024.06.28 |
댓글