简体   繁体   English

Visual Basic自定义未处理的异常

[英]Visual Basic Customize Unhandled Exceptions

So I have a Visual Basic application created in Visual Studio 2013 and I am trying to find a way to gracefully handle unhandled exceptions. 因此,我在Visual Studio 2013中创建了一个Visual Basic应用程序,我试图找到一种方法来优雅地处理未处理的异常。 I have graceful failing covered pretty well in the application but want to plan for something I missed. 我在应用程序中很好地解决了失败问题,但是想为我错过的事情做计划。

So, that said. 这么说。 Is there a way to not show a user an unhandled exception directly but rather trigger a function or class that will display a friendly message to the user and write the technical stuff to an error log? 有没有一种方法不直接向用户显示未处理的异常,而是触发将向用户显示友好消息并将技术内容写入错误日志的函数或类?

UI frameworks typically have a default handler in their main event loop which raises an event if an exception is thrown inside the loop, but the details vary depending on the framework. UI框架通常在其主事件循环中有一个默认处理程序,如果在循环中引发异常,该处理程序会引发一个事件,但是具体情况取决于框架。 If your application is WPF-based, unhandled exceptions on the UI thread will raise Application.DispatcherUnhandledException . 如果您的应用程序是基于WPF的,则UI线程上未处理的异常将引发Application.DispatcherUnhandledException If it is WinForms-based, unhandled exceptions on the UI thread will raise Application.ThreadException . 如果它是基于WinForms的,则UI线程上未处理的异常将引发Application.ThreadException

The .Net runtime will raise AppDomain.UnhandledException on exceptions that aren't handled anywhere on the call stack (such as from worker threads). .Net运行时将引发未在调用堆栈上的任何地方处理的异常(例如,来自工作线程的异常)的AppDomain.UnhandledException

Assuming Winforms, try this: On the 'Application' tab of the Project Properties window, click the 'View Application Events' button towards the bottom. 假设使用Winforms,请尝试以下操作:在“项目属性”窗口的“应用程序”选项卡上,单击底部的“查看应用程序事件”按钮。 Within the partial class that's generated for you, create a sub like this: 在为您生成的部分类中,创建一个如下子:

 Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
...
end sub

I'll leave the friendly message and logging to you. 我会留下友好的消息并登录到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM