如何在VB.Net中禁用错误​​对话框

时间:2016-02-10 22:31:05

标签: c# .net vb.net exception exception-handling

如果应用创建例外,Windows会暂停并显示错误对话框。用户可以决定关闭该应用程序。

如何禁用此框? (是的,我知道Try,Catch,最后但是我想为应用程序全局禁用它。)我想显示MyErrorForm.vb而不是标准错误框,或者禁用它。

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

将代码模块文件添加到项目中,将其命名为UnhandledExceptions.vb,然后添加以下代码以替换默认内容。

Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
    Partial Friend Class MyApplication
        ''' <summary>
        ''' Indicates if we are running under the IDE or not
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property RunningUnderDebugger() As Boolean
            Get
                Return System.Diagnostics.Debugger.IsAttached
            End Get
        End Property
        Private mUnhandledExceptionsFileName As String
        Public Property UnhandledExceptionsFileName() As String
            Get
                Return mUnhandledExceptionsFileName
            End Get
            Set(ByVal value As String)
                mUnhandledExceptionsFileName = value
            End Set
        End Property
        Private mExceptionDialogIcon As Icon
        ''' <summary>
        ''' Specifically used to set the exception dialog's icon
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ExceptionDialogIcon() As Icon
            Get
                Return mExceptionDialogIcon
            End Get
            Set(ByVal value As Icon)
                mExceptionDialogIcon = value
            End Set
        End Property

        Private mContinueAfterException As Boolean
        ''' <summary>
        ''' Determine if this app can stay open after an unhandled
        ''' exception has been thrown.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>
        ''' Not practical in most circumstances as we usually
        ''' do not know how to hangle unhandled exceptions and
        ''' leaving an app open will not be wise.
        ''' </remarks>
        Public Property ContinueAfterException() As Boolean
            Get
                Return Not mContinueAfterException
            End Get
            Set(ByVal value As Boolean)
                mContinueAfterException = value
            End Set
        End Property
        ''' <summary>
        ''' Displays a user friendly message in regards to the 
        ''' unhandled exception.
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks>
        ''' It would be wise to also write to a log file etc.
        ''' 
        ''' WARNING 
        ''' If you use code prone to errors in here then you will not 
        ''' be very happy so test test test before implementing.
        ''' </remarks>
        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Handles Me.UnhandledException

            If String.IsNullOrEmpty(mUnhandledExceptionsFileName) Then
                mUnhandledExceptionsFileName = "unHandledExceptions.xml"
            End If

            Dim Doc As New XDocument

            If IO.File.Exists(My.Application.UnhandledExceptionsFileName) Then
                Doc = XDocument.Load(My.Application.UnhandledExceptionsFileName)
            Else
                Doc = XDocument.Parse("<?xml version=""1.0"" encoding=""utf-8""?><Exceptions></Exceptions>")
            End If

            Dim Content = Doc.<Exceptions>(0)
            Dim StackTraceText As String = e.Exception.StackTrace.ToString

            Content.Add(
            <Exception>
                <Date_Time><%= Now.ToString("MM/dd/yyyy HH:mm") %></Date_Time>
                <Message><%= e.Exception.Message %></Message>
                <StackTrace><%= Environment.NewLine %>
                    <%= StackTraceText %><%= Environment.NewLine %>
                </StackTrace>
            </Exception>)

            Content.Save(My.Application.UnhandledExceptionsFileName)

            ' here I am using a form setup specifically to show exceptions
            'Dim f As New frmExceptionDialog
            'f.InBoundException = e.Exception
            'f.Message = String.Format("Exception has been recorded to [{0}]", My.Application.UnhandledExceptionsFileName)
            'f.Header = "Unhandled exception"


            'Try
            '    f.ShowDialog()
            'Finally
            '    f.Dispose()
            'End Try

            MessageBox.Show("Encountered an exception" & Environment.NewLine & e.Exception.Message)


            e.ExitApplication = Me.ContinueAfterException

            If Me.ContinueAfterException Then
                Me.ContinueAfterException = False
            End If

        End Sub
    End Class
End Namespace

启动表格中的使用示例

Public Class Form1
    Private ExceptionLogFile As String = ""
    Public Sub New()
        InitializeComponent()
        My.Application.UnhandledExceptionsFileName = "unHandledExceptions.xml"
        ExceptionLogFile = My.Application.UnhandledExceptionsFileName
    End Sub
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If My.Application.RunningUnderDebugger Then
            MessageBox.Show("Please execute from Window's Explorer")
            Application.ExitThread()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Throw New Exception("Hey")
    End Sub
End Class

生成的输出

<?xml version="1.0" encoding="utf-8"?>
<Exceptions>
  <Exception>
    <Date_Time>02/10/2016 15:45</Date_Time>
    <Message>Hey</Message>
    <StackTrace>
   at WindowsApplication3.Form1.Button1_Click(Object sender, EventArgs e) in C:\Dotnet_Development\Projects_Non_Business\WindowsApplication3\Form1.vb:line 16
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message&amp; m)
   at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
   at System.Windows.Forms.Button.WndProc(Message&amp; m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
</StackTrace>
  </Exception>
</Exceptions>

我使用了xml,但您可以轻松使用文本文件或其他类型的文件。当我做这种类型的异常工作时,代码也会给我发一封电子邮件。

另请参阅哪个自定义表单显示消息而不是MessageBox。 Dealing with unhandled exceptions in Window's form solutions

重要提示:在第一个文件中有一个属性ContinueAfterException,在99%的时间内将其设置为false,因为将其设置为true将允许(如果可能)应用程序保持活动状态但更可能不稳定。

答案 2 :(得分:0)

我找到了答案!在Program.cs(主文件)中输入以下代码:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

(将其放在Application.Run之前)和广告错误处理:

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
     //TODO: Handle error...
}
相关问题