Resume program execution after handling an unhandled exception

℡╲_俬逩灬. 提交于 2019-12-08 10:20:00

问题


Question: I catch generally unhandled exceptions with AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException

The problem now is, with this exception handler

Public Sub OnUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
    Console.WriteLine(e.ExceptionObject.ToString())
    Console.WriteLine("Press Enter to continue")
    Console.ReadLine()
    'Environment.Exit(1)
End Sub

program execution still throws the exception if I don't exit the program.
Now I want to know how I can change that to a 'on error resume next' behaviour ? Is it possible at all ?

    <STAThread()> _
Public Sub Main(ByVal argv As String())
    'For i As Integer = 0 To argv.Length - 1 Step 1
    'Console.WriteLine("Argument {0}: {1}", i, argv(i))
    'Next i

    AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException

    Throw New Exception("Test")
    Console.WriteLine("Continue")
End sub

回答1:


If you want to do anything beyond quickly logging or alerting the user of an exception before the program exits, you need to handle the exception normally with a try/catch block. Exceptions should be handled as soon as possible to make a real decision about how to react. UnhandledException may sound like it would be a great place to centralize all your exception handling, but that's not it's intended purpose and would get very complicated if it had to handle every exception thrown anywhere in the application.



来源:https://stackoverflow.com/questions/3157553/resume-program-execution-after-handling-an-unhandled-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!