Wpf Application.Current.DispatcherUnhandledException
我想捕获一个未处理的异常,该异常导致我的应用程序以静默方式关闭 我读到有Wpf Application.Current.DispatcherUnhandledException,wpf,vb.net,exception,visual-studio-2012,exception-handling,Wpf,Vb.net,Exception,Visual Studio 2012,Exception Handling,我想捕获一个未处理的异常,该异常导致我的应用程序以静默方式关闭 我读到有 Application.Current.DispatcherUnhandledException 然而,我的应用程序使用一个窗体来运行,并使用应用程序框架,而不是一个子主键 在我看来,MSDN()上的示例似乎依赖于sub-main 有人能告诉我如何为使用应用程序框架的项目安装异常处理程序吗 我尝试了以下方法: 我已将应用程序更改为使用Sub-Main,并使用了以下代码: Public Sub Main() '
Application.Current.DispatcherUnhandledException
然而,我的应用程序使用一个窗体来运行,并使用应用程序框架,而不是一个子主键
在我看来,MSDN()上的示例似乎依赖于sub-main
有人能告诉我如何为使用应用程序框架的项目安装异常处理程序吗
我尝试了以下方法:
我已将应用程序更改为使用Sub-Main,并使用了以下代码:
Public Sub Main()
' Set the unhandled exception mode to force all Windows Forms errors to go through'
' our handler. '
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
' Add the event handler for handling UI thread exceptions to the event. '
AddHandler Application.ThreadException, AddressOf frmMain.UIThreadException
' Add the event handler for handling non-UI thread exceptions to the event. '
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf frmMain.CurrentDomain_UnhandledException
' Runs the application. '
Application.Run(New frmMain())
End Sub
但是,我得到的错误是:
"The thread exception mode can no longer be changed as soon as controls were created in this thread".
您可以使用Try-Catch来避免应用程序因异常而关闭。你可以试试类似的东西
Try
Console.WriteLine("Hello, world!")
Dim A As Integer = 0
Dim I As Integer
For I = 1 to 500000000000
A*=I
Next
Catch ex As Exception
Console.WriteLine("Error occurred")
End Try
这是控制台应用程序中的一个小示例,但您可以在项目中使用类似的内容。
通用异常捕获您可能遇到的每种异常,Application.Current.dispatchernhandledexception也是如此。
您的程序不应该再使用try语句崩溃
希望能有所帮助。在运行时未捕获某些异常的情况下,最好也注册一个事件处理程序 它允许您记录异常原因并以受控方式退出应用程序
但是正如前面所说的,您应该首先使用Try-Catch语句来捕获异常。请不要只要求我们为您解决问题。向我们展示你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅“”,以获取您真正需要阅读的优秀文章。你在哪里看到了关于
Sub-Main
?@JohnSaunders我已经相应地编辑了我的文章。我们必须看不同的例子。我正在查看的未使用Sub-Main
。Try/Catch无法处理我的错误类型。我知道崩溃发生在哪里。请向我们提供完整的源代码,或者至少解释一下你在做什么。