프로그램 예외 메시지 상자

응용 프로그램에서 예외 메시지 상자를 사용하면 MessageBox 클래스에 비해 메시징 환경을 보다 강력하게 제어할 수 있습니다. 자세한 내용은 예외 메시지 상자 프로그래밍를 참조하세요. 예외 메시지 상자 .dll 가져오기 및 배포에 대한 자세한 내용은 예외 메시지 상자 응용 프로그램 배포를 참조하십시오.

절차

예외 메시지 상자를 사용하여 예외를 처리하려면

  1. 관리 코드 프로젝트의 참조를 Microsoft.ExceptionMessageBox.dll 어셈블리에 추가합니다.

  2. (옵션) Microsoft.SqlServer.MessageBox 네임스페이스를 사용하도록 using(C#) 또는 Imports(Microsoft Visual Basic .NET) 지시어를 추가합니다.

  3. 예상되는 예외를 처리할 try-catch 블록을 만듭니다.

  4. catch 블록 안에 ExceptionMessageBox 클래스의 인스턴스를 만듭니다. try-catch 블록에서 처리하는 Exception 개체를 전달합니다.

  5. (옵션) ExceptionMessageBox에 다음 속성 중 하나 이상을 설정합니다.

  6. Show 메서드를 호출합니다. 예외 메시지 상자가 속해 있는 부모 창을 전달합니다.

  7. (옵션) 사용자가 어느 단추를 클릭했는지 확인하려면 반환된 DialogResult 열거형의 값을 확인합니다.

예외 없이 예외 메시지 상자를 표시하려면

  1. 관리 코드 프로젝트의 참조를 Microsoft.ExceptionMessageBox.dll 어셈블리에 추가합니다.

  2. (옵션) Microsoft.SqlServer.MessageBox 네임스페이스를 사용하도록 using(C#) 또는 Imports(Visual Basic .NET) 지시어를 추가합니다.

  3. ExceptionMessageBox 클래스의 인스턴스를 만듭니다. 메시지 텍스트를 String 값으로 전달합니다.

  4. (옵션) ExceptionMessageBox에 다음 속성 중 하나 이상을 설정합니다.

  5. Show 메서드를 호출합니다. 예외 메시지 상자가 속해 있는 부모 창을 전달합니다.

  6. (옵션) 사용자가 어느 단추를 클릭했는지 확인하려면 반환된 DialogResult 열거형의 값을 확인합니다.

사용자 지정된 단추와 함께 예외 메시지 상자를 표시하려면

  1. 관리 코드 프로젝트의 참조를 Microsoft.ExceptionMessageBox.dll 어셈블리에 추가합니다.

  2. (옵션) Microsoft.SqlServer.MessageBox 네임스페이스를 사용하도록 using(C#) 또는 Imports(Visual Basic .NET) 지시어를 추가합니다.

  3. 다음의 두 가지 방법 중 하나로 ExceptionMessageBox 클래스의 인스턴스를 만듭니다.

    • try-catch 블록에서 처리하는 Exception 개체를 전달합니다.

    • 메시지 텍스트를 String 값으로 전달합니다.

  4. Buttons에 다음 값 중 하나를 설정합니다.

    • AbortRetryIgnore - 중단, 다시 시도무시 단추를 표시합니다.

    • Custom - 사용자 지정 단추를 표시합니다.

    • OK - 확인 단추를 표시합니다.

    • OKCancel - 확인취소 단추를 표시합니다.

    • RetryCancel - 다시 시도취소 단추를 표시합니다.

    • YesNo - 아니요 단추를 표시합니다.

    • YesNoCancel - , 아니요취소 단추를 표시합니다.

  5. (옵션) 사용자 지정 단추를 사용할 경우 SetButtonText 메서드의 오버로드 중 하나를 호출하여 최대 5개의 사용자 지정 단추에 대해 텍스트를 지정할 수 있습니다.

  6. Show 메서드를 호출합니다. 예외 메시지 상자가 속해 있는 부모 창을 전달합니다.

  7. (옵션) 사용자가 어느 단추를 클릭했는지 확인하려면 반환된 DialogResult 열거형의 값을 확인합니다. 사용자 지정 단추를 사용할 경우 사용자가 어느 사용자 지정 단추를 클릭했는지 확인하려면 CustomDialogResult 속성의 ExceptionMessageBoxDialogResult 값을 확인합니다.

예외 메시지 상자의 표시 여부를 사용자가 결정할 수 있게 하려면

  1. 관리 코드 프로젝트의 참조를 Microsoft.ExceptionMessageBox.dll 어셈블리에 추가합니다.

  2. (옵션) Microsoft.SqlServer.MessageBox 네임스페이스를 사용하도록 using(C#) 또는 Imports(Visual Basic .NET) 지시어를 추가합니다.

  3. 다음의 두 가지 방법 중 하나로 ExceptionMessageBox 클래스의 인스턴스를 만듭니다.

    • try-catch 블록에서 처리하는 Exception 개체를 전달합니다.

    • 메시지 텍스트를 String 값으로 전달합니다.

  4. ShowCheckbox 속성을 true로 설정합니다.

  5. (옵션) 예외 메시지 상자를 다시 표시할지 여부를 사용자에게 묻는 텍스트를 CheckboxText에 지정합니다. 기본 텍스트는 "이 메시지를 다시 표시 안 함"입니다.

  6. 사용자가 선택한 내용을 응용 프로그램의 실행 기간 동안에만 유지하려면 IsCheckboxChecked 값을 전역 Boolean 변수로 설정합니다. 예외 메시지 상자의 인스턴스를 만들기 전에 이 값을 확인하십시오.

  7. 사용자가 선택한 내용을 영구적으로 저장하려면 다음과 같이 합니다.

    1. CreateSubKey(String) 메서드를 호출하여 응용 프로그램에서 사용하는 사용자 지정 레지스트리 키를 열고, CheckboxRegistryKey를 반환되는 RegistryKey 개체로 설정합니다.

    2. CheckboxRegistryValue를 사용되는 레지스트리 값의 이름으로 설정합니다.

    3. CheckboxRegistryMeansDoNotShowDialog를 true로 설정합니다.

    4. Show 메서드를 호출합니다. 지정된 레지스트리 키가 확인되고, 레지스트리 키에 저장된 데이터가 0인 경우에만 예외 메시지 상자가 표시됩니다. 대화 상자가 표시되고 사용자가 단추를 클릭하기 전에 확인란을 선택하면 레지스트리 키의 데이터가 1로 설정됩니다.

이 예에서는 확인 단추만 있는 예외 메시지 상자를 사용하여 처리된 예외 및 추가적인 응용 프로그램 관련 정보가 포함된 응용 프로그램 예외 정보를 표시합니다.

         try
            {
                // Do something that may generate an exception.
                throw new ApplicationException("An error has occured");
            }
            catch (ApplicationException ex)
            {
                // Define a new top-level error message.
                string str = "The action failed.";

                // Add the new top-level message to the handled exception.
                ApplicationException exTop = new ApplicationException(str, ex);
                exTop.Source = this.Text;

                // Show an exception message box with an OK button (the default).
                ExceptionMessageBox box = new ExceptionMessageBox(exTop);
                box.Show(this);
            }
Try
    ' Do something that may generate an exception.
    Throw New ApplicationException("An error has occured")
Catch ex As ApplicationException
    ' Define a new top-level error message.
    Dim str As String = "The action failed."

    ' Add the new top-level message to the handled exception.
    Dim exTop As ApplicationException = New ApplicationException(str, ex)
    exTop.Source = Me.Text

    ' Show an exception message box with an OK button (the default).
    Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)
    box.Show(Me)
End Try

이 예에서는 사용자가 선택할 수 있는 아니요 단추가 포함된 예외 메시지 상자를 사용합니다.

          // Define the message and caption to display.
            string str = @"Are you sure you want to delete file 'c:\somefile.txt'?";
            string caption = "Confirm File Deletion";

            // Show the exception message box with Yes and No buttons.
            ExceptionMessageBox box = new ExceptionMessageBox(str,
                caption, ExceptionMessageBoxButtons.YesNo,
                ExceptionMessageBoxSymbol.Question,
                ExceptionMessageBoxDefaultButton.Button2);

            if (DialogResult.Yes == box.Show(this))
            {
                // Delete the file.
            }
' Define the message and caption to display.
Dim str As String = "Are you sure you want to delete file 'c:\somefile.txt'?"
Dim caption As String = "Confirm File Deletion"

' Show the exception message box with Yes and No buttons.
Dim box As ExceptionMessageBox = New ExceptionMessageBox(str, _
 caption, ExceptionMessageBoxButtons.YesNo, _
 ExceptionMessageBoxSymbol.Question, _
 ExceptionMessageBoxDefaultButton.Button2)

If Windows.Forms.DialogResult.Yes = box.Show(Me) Then
    ' Delete the file.
End If

이 예에서는 사용자 지정 단추가 포함된 예외 메시지 상자를 사용합니다.

           try
            {
                // Do something that may cause an exception.
                throw new ApplicationException("An error has occured");
            }
            catch (ApplicationException ex)
            {
                string str = "Action failed. What do you want to do?";
                ApplicationException exTop = new ApplicationException(str, ex);
                exTop.Source = this.Text;

                // Show the exception message box with three custom buttons.
                ExceptionMessageBox box = new ExceptionMessageBox(exTop);

                // Set the names of the three custom buttons.
                box.SetButtonText("Skip", "Retry", "Stop Processing");

                // Set the Retry button as the default.
                box.DefaultButton = ExceptionMessageBoxDefaultButton.Button2;
                box.Symbol = ExceptionMessageBoxSymbol.Question;
                box.Buttons = ExceptionMessageBoxButtons.Custom;

                box.Show(this);

                // Do something, depending on the button that the user clicks.
                switch (box.CustomDialogResult)
                {
                    case ExceptionMessageBoxDialogResult.Button1:
                        // Skip action
                        break;
                    case ExceptionMessageBoxDialogResult.Button2:
                        // Retry action
                        break;
                    case ExceptionMessageBoxDialogResult.Button3:
                        // Stop processing action
                        break;
                }
            }
Try
    ' Do something that may cause an exception.
    Throw New ApplicationException("An error has occured")
Catch ex As ApplicationException
    Dim str As String = "Action failed. What do you want to do?"
    Dim exTop As ApplicationException = New ApplicationException(str, ex)
    exTop.Source = Me.Text

    ' Show the exception message box with three custom buttons.
    Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)

    ' Set the names of the three custom buttons.
    box.SetButtonText("Skip", "Retry", "Stop Processing")

    ' Set the Retry button as the default.
    box.DefaultButton = ExceptionMessageBoxDefaultButton.Button2
    box.Symbol = ExceptionMessageBoxSymbol.Question
    box.Buttons = ExceptionMessageBoxButtons.Custom

    box.Show(Me)

    ' Do something, depending on the button that the user clicks.
    Select Case box.CustomDialogResult
        Case ExceptionMessageBoxDialogResult.Button1
            ' Skip action
        Case ExceptionMessageBoxDialogResult.Button2
            ' Retry action
        Case ExceptionMessageBoxDialogResult.Button3
            ' Stop processing action
    End Select
End Try

이 예에서는 확인란을 사용하여 예외 메시지 상자의 표시 여부를 결정합니다.

            try
            {
                // Do something that may cause an exception.
                throw new ApplicationException("An error has occured.");
            }
            catch (ApplicationException ex)
            {
                string str = "The action failed.";
                ApplicationException exTop = new ApplicationException(str, ex);
                exTop.Source = this.Text;

                // Show a message box if the global variable is true.
                if (alwaysShow)
                {
                    ExceptionMessageBox box = new ExceptionMessageBox(exTop);
                    box.ShowCheckBox = true;
                    box.IsCheckBoxChecked = true;
                    box.CheckBoxText = "Always show this message";
                    box.Show(this);

                    // Set the global variable.
                    alwaysShow = box.IsCheckBoxChecked;
                }
            }
Try
    ' Do something that may cause an exception.
    Throw New ApplicationException("An error has occured.")
Catch ex As ApplicationException
    Dim str As String = "The action failed."
    Dim exTop As ApplicationException = New ApplicationException(str, ex)
    exTop.Source = Me.Text

    ' Show a message box if the global variable is true.
    If alwaysShow Then
        Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)
        box.ShowCheckBox = True
        box.IsCheckBoxChecked = True
        box.CheckBoxText = "Always show this message"
        box.Show(Me)

        ' Set the global variable.
        alwaysShow = box.IsCheckBoxChecked
    End If
End Try

이 예에서는 확인란과 레지스트리 키를 사용하여 예외 메시지 상자의 표시 여부를 결정합니다.

           try
            {
                // Do something that could generate an exception.
                throw new ApplicationException("An error has occured.");
            }
            catch (ApplicationException ex)
            {
                string str = "The action failed. Do you want to continue?";
                ApplicationException exTop = new ApplicationException(str, ex);
                exTop.Source = this.Text;

                // Show a message box with Yes and No buttons
                ExceptionMessageBox box = new ExceptionMessageBox(exTop,
                    ExceptionMessageBoxButtons.YesNo,
                    ExceptionMessageBoxSymbol.Question,
                    ExceptionMessageBoxDefaultButton.Button2);

                // Enable the check box.
                box.ShowCheckBox = true;

                // Define the registry key to use.
                box.CheckBoxRegistryKey =
                    Microsoft.Win32.Registry.CurrentUser.CreateSubKey(
                    @"Software\TestApp");
                box.CheckBoxRegistryValue = "DontShowActionFailedMessage";
                box.CheckBoxRegistryMeansDoNotShowDialog = true;
                box.DefaultDialogResult = DialogResult.Yes;

                // The message box won�t be displayed if the
                // "DontShowActionFailedMessage" value of the registry key 
                // contains a non-zero value.
                if (box.Show(this) == DialogResult.No)
                {
                    // Do something if the user clicks the No button.
                    this.Close();
                }
            }
Try
    ' Do something that could generate an exception.
    Throw New ApplicationException("An error has occured.")
Catch ex As ApplicationException
    Dim str As String = "The action failed. Do you want to continue?"
    Dim exTop As ApplicationException = New ApplicationException(str, ex)
    exTop.Source = Me.Text

    ' Show a message box with Yes and No buttons
    Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop, _
     ExceptionMessageBoxButtons.YesNo, _
     ExceptionMessageBoxSymbol.Question, _
     ExceptionMessageBoxDefaultButton.Button2)

    ' Enable the check box.
    box.ShowCheckBox = True

    ' Define the registry key to use.
    box.CheckBoxRegistryKey = _
    Microsoft.Win32.Registry.CurrentUser.CreateSubKey( _
     "Software\TestApp")
    box.CheckBoxRegistryValue = "DontShowActionFailedMessage"
    box.CheckBoxRegistryMeansDoNotShowDialog = True
    box.DefaultDialogResult = Windows.Forms.DialogResult.Yes

    ' The message box won�t be displayed if the
    ' "DontShowActionFailedMessage" value of the registry key 
    ' contains a non-zero value.
    If box.Show(Me) = Windows.Forms.DialogResult.No Then
        ' Do something if the user clicks the No button.
        Me.Close()
    End If
End Try

이 예에서는 예외 메시지 상자를 사용하여 문제 해결이나 디버깅에 도움이 되는 추가적인 정보를 표시합니다.

           try
            {
                // Do something that you don't expect to generate an exception.
                throw new ApplicationException("Failed to connect to the server.");
            }
            catch (ApplicationException ex)
            {
                string str = "An unexpected error occurred. Please call Helpdesk.";
                ApplicationException exTop = new ApplicationException(str, ex);
                exTop.Source = this.Text;

                // Information in the Data property of an exception that has a name
                // beginning with "HelpLink.Advanced" is shown when the user
                // clicks the Advanced Information button of the exception message
                // box dialog box.
                exTop.Data.Add("AdvancedInformation.FileName", "application.dll");
                exTop.Data.Add("AdvancedInformation.FilePosition", "line 355");
                exTop.Data.Add("AdvancedInformation.UserContext", "single user mode");

                // Show the exception message box with additional information that 
                // is helpful when a user calls technical support.
                ExceptionMessageBox box = new ExceptionMessageBox(exTop);

                box.Show(this);
            }
Try
    ' Do something that you don't expect to generate an exception.
    Throw New ApplicationException("Failed to connect to the server.")
Catch ex As ApplicationException
    Dim str As String = "An unexpected error occurred. Please call Helpdesk."
    Dim exTop As ApplicationException = New ApplicationException(str, ex)
    exTop.Source = Me.Text

    ' Information in the Data property of an exception that has a name
    ' beginning with "HelpLink.Advanced" is shown when the user
    ' clicks the Advanced Information button of the exception message
    ' box dialog box.
    exTop.Data.Add("AdvancedInformation.FileName", "application.dll")
    exTop.Data.Add("AdvancedInformation.FilePosition", "line 355")
    exTop.Data.Add("AdvancedInformation.UserContext", "single user mode")

    ' Show the exception message box with additional information that 
    ' is helpful when a user calls technical support.
    Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)

    box.Show(Me)

End Try