My.Application.UnhandledException 事件
更新:2007 年 11 月
在应用程序遇到未处理的异常时会发生此事件。
' Usage
Public Sub Me_UnhandledException( _
ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs _
) Handles Me.UnhandledException
End Sub
' Declaration
Public Event UnhandledException( _
ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs _
)
参数
sender
引发该事件的 Object。e
包含未处理异常及其他信息的 UnhandledExceptionEventArgs 对象。
备注
应用程序在遇到未处理的异常时会引发 UnhandledException 事件。此事件是 Visual Basic 应用程序模型的一部分。有关更多信息,请参见Visual Basic 应用程序模型概述。
使用 e 参数的 Exception 属性可访问引发此事件的未处理异常。
使用 e 参数的 ExitApplication 属性可控制应用程序是否退出。ExitApplication 默认为 True,因此应用程序在完成 UnhandledException 事件处理程序后退出。您可以在 UnhandledException 事件处理程序中将该值设为 False 以保持应用程序处于运行状态,并且可以让它返回到等待状态。
UnhandledException 事件处理程序的代码存储在 ApplicationEvents.vb 文件中,此文件默认情况下是隐藏的。
访问应用程序事件的“代码编辑器”窗口:
当“解决方案资源管理器”中有项目选中时,在“项目”菜单上单击“属性”。
单击“应用程序”选项卡。
单击“查看应用程序事件”按钮以打开代码编辑器。
有关更多信息,请参见如何:处理应用程序事件 (Visual Basic)。
说明: |
---|
为了让调试器能够处理未处理的异常,Visual Basic 编译器禁止为调试生成的应用程序引发此事件。这意味着,如果通过在“Visual Studio 集成开发环境”调试器下运行应用程序来测试它,则不会调用 UnhandledException 事件处理程序。有关为调试生成应用程序的更多信息,请参见 /debug (Visual Basic)。 |
任务
下表列出了涉及 My.Application.UnhandledException 事件的任务示例。
若要执行以下操作 |
请参见 |
---|---|
使用“Visual Basic 应用程序模型”提供的事件运行代码 |
|
记录未处理的异常 |
示例
此示例使用 My.Application.UnhandledException 事件记录所有未处理的异常。
Private Sub MyApplication_UnhandledException( _
ByVal sender As Object, _
ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs _
) Handles Me.UnhandledException
My.Application.Log.WriteException(e.Exception, _
TraceEventType.Critical, _
"Unhandled Exception.")
End Sub
必须在“代码编辑器”窗口中输入应用程序事件的代码。若要访问此窗口,请按照本主题“备注”部分所述的步骤操作。有关更多信息,请参见如何:处理应用程序事件 (Visual Basic)。
由于应用程序在附加了调试器时不引发 UnhandledException 事件,因此需要在 Visual Studio 集成开发环境以外运行此示例。
要求
命名空间:Microsoft.VisualBasic.ApplicationServices
**程序集:**Visual Basic 运行库(位于 Microsoft.VisualBasic.dll 中)
按项目类型列出的可用性
项目类型 |
是否可用 |
---|---|
Windows 应用程序 |
是 |
类库 |
否 |
控制台应用程序 |
否 |
Windows 控件库 |
否 |
Web 控件库 |
否 |
Windows 服务 |
否 |
网站 |
否 |
权限
可能需要下列权限:
权限 |
说明 |
---|---|
控制为此事件添加事件处理程序的能力。关联枚举:SecurityPermissionFlag.ControlAppDomain。 |
请参见
概念
参考
Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs
修订记录
日期 |
修订 |
原因 |
---|---|---|
2008 年 7 月 |
修订了“备注”部分有关设置 UnhandledException 事件处理程序的信息。 |
内容 Bug 修复 |