处理未处理的异常后恢复程序执行 [英] Resume program execution after handling an unhandled exception

查看:126
本文介绍了处理未处理的异常后恢复程序执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:我赶上与一般未处理的异常 的AddHandler System.AppDomain.CurrentDomain.UnhandledException,AddressOf OnUnhandledException

现在的问题是,这个异常处理程序

 公用Sub OnUnhandledException(BYVAL发件人为对象,BYVAL E上UnhandledExceptionEventArgs)
    Console.WriteLine(e.ExceptionObject.ToString())
    Console.WriteLine(preSS回车键继续)
    到Console.ReadLine()
    'Environment.Exit(1)
结束小组
 

执行程序仍引发异常,如果我不退出程序。
现在我想知道我怎么可以更改为关于 错误 &简历NBSP;接下来的行为?是否有可能呢?

 < STAThread()> _
公用Sub主要(argv的BYVAL作为字符串())
    对于我作为整数= 0〜argv.Length  -  1第1步
    Console.WriteLine(参数{0}:{1},我,argv的(I))
    下一步我

    AddHandler的System.AppDomain.CurrentDomain.UnhandledException,AddressOf OnUnhandledException

    抛出新的异常(测试)
    Console.WriteLine(继续)
完子
 

解决方案

如果你想做的任何事情以后迅速登录或者程序退出前警告异常的用户,则需要通常处理异常与的try / catch 块。例外情况,应尽快处理,以就如何应对一个真正的决定。 UnhandledException 听起来像这将是一个伟大的地方集中所有的异常处理,但这不是它的预期目的,将变得非常复杂,如果它必须处理每一个异常抛出的任何地方在应用程序中。

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

解决方案

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.

这篇关于处理未处理的异常后恢复程序执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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