검색결과 리스트
예외처리에 해당되는 글 1건
- 2014.04.30 [.NET] 처리하지 못한 예외를 관리
Windows Forms
처리하지 못한 예외가 발생한 경우 Application.ThreadException 이벤트 핸들러가 자동으로 호출된다. 그래서 여기에 에러 메시지를 보여주는 기능 등을 작성하면 된다.
예)
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show("처리하지 못한 예외 발생");
Application.Exit();
}
}
}
Console
AppDomain.CurrentDomain.UnhandledException 이벤트 핸들러를 자동으로 호출한다.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
MessageBox.Show("처리하지 못한 예외 발생");
Environment.Exit(-1);
}
}
}
댓글