Application.ThreadException和AppDomain.CurrentDomain.UnhandledException? [英] Application.ThreadException and AppDomain.CurrentDomain.UnhandledException?

查看:83
本文介绍了Application.ThreadException和AppDomain.CurrentDomain.UnhandledException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主过程中有下一个代码:

I have the next code in my Main procedure:

<STAThread()>
Public Shared Sub main(ByVal args() As String)
    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)

    Dim appdomain As AppDomain = appdomain.CurrentDomain
    AddHandler appdomain.UnhandledException, New UnhandledExceptionEventHandler(AddressOf UnhandledExceptions)

    AddHandler Application.ThreadException, New ThreadExceptionEventHandler(AddressOf UnhandledExceptions)
    
    'Main Try Catch Block
    Try
       Application.Run(New FormWelcome())
    Catch ex As Exception
       MessageBox.Show(ex.Message, "Main Try Catch Block", MessageBoxButtons.OK, MessageBoxIcon.Error)
       Application.Exit()
    End Try
End Sub

Public Shared Sub UnhandledExceptions(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
    MessageBox.Show(e.Exception.Message, "Unhandled Main Thread Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub

Public Shared Sub GcsUnhandledExceptions(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
    MessageBox.Show(CType(e.ExceptionObject, Exception).Message, "Unhandled Domain Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Sub



我对未处理的异常有疑问:

1.-在上面的代码中,如果我从FormWelcome抛出了一个新的未处理的异常(ex),则ex由Application.ThreadException处理,但是如果我为Application.ThreadException注释了AddHandler,则ex由Main Try Catch处理.堵塞.为什么第一个ex不由Main Try Catch块处理?

2.-有一种方法可以避免在出现appdomain.UnhandledException?时显示Windows调试对话框?

希望有人可以帮助我,谢谢.



I have some question about unhandled exceptions:

1.- In the above code if I throw a new unhandled exception(ex) from the FormWelcome the ex is handled by the Application.ThreadException, but if I comment the AddHandler for Application.ThreadException, the ex is handled by the Main Try Catch Block. Why the first ex isn''t handled by the Main Try Catch Block?

2.- There is a way to avoid showing the Windows debug dialog when ocurrs an appdomain.UnhandledException?

I hope somebody can help me, thanks

推荐答案

您需要再添加一个步骤:
You need to add one more step:
using System.Windows.Forms;

//...

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

当您在一开始就将其添加到某个位置时(通常是在我的应用程序入口点,通常是Main),它将完全按照您的要求工作.

—SA

When you add it somewhere in the very beginning (I usually do it in my application entry point, usually Main), it will work exactly as you wanted.

—SA


这篇关于Application.ThreadException和AppDomain.CurrentDomain.UnhandledException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆