본문 바로가기
C#/Winform

프로그램 실행 중 처리 하지 못한 예외 관리.

by HyunS_ 2019. 2. 17.
728x90

코드 작성 중 모든 예외를 다 처리할 수는 없을 것이다.


예상치 못한 예외가 발생했을 때 알 수 있도록 메시지로 출력되도록 할 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
static void Main()
{
    Application.ThreadException += Application_ThreadException;
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
}
 
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    MessageBox.Show($"{e.Exception.Message}{Constants.HardCoding.lineDivision}{e.Exception.StackTrace}",
                     Constants.HardCoding.messageCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
 
cs


Application.ThreadException  이벤트를 추가하고 그 안에 메시지를 출력하는 코드를 작성하면 된다.

728x90

댓글