简体   繁体   English

如何在未处理的异常后继续运行?

[英]How do I continue running after an unhandled exception?

I have the following code in my application that is running after an unhandled exception: 我的应用程序中有以下代码在未处理的异常之后运行:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var exception = e.ExceptionObject as Exception;
            if (exception != null) MessageBox.Show(exception.Message + " - " + exception.StackTrace);
        }

but even if i catch unhandled exception my windows mobile application close. 但即使我抓住未处理的异常我的Windows移动应用程序关闭。 How to prevent closing application when i catch unhandled exception. 当我捕获未处理的异常时如何防止关闭应用程序。 I never want to close my app. 我永远不想关闭我的应用程序。 I want to open Login form in this scenario or anything else not close app. 我想在这个场景中打开登录表单或其他任何不关闭应用程序的表单。

So what i want is to prevent closing application from unhandled exception like network is down,... 所以我想要的是防止关闭应用程序从未处理的异常,如网络关闭,...

I can not use try catch in every code .... 我不能在每个代码中使用try catch ....

Any idea how to prevent closing app when network is down or any other unhandled exceptions? 知道如何在网络中断或任何其他未处理的异常时阻止关闭应用程序吗?

You don't. 你没有。 When you get an AppDomain unhandled exception, your app is no longer in a stable state. 当您收到AppDomain未处理的异常时,您的应用不再处于稳定状态。 Where exactly would you resume to? 你究竟要回到哪里? When you've gotten to that point, your only option, with good reason, is to exit. 当你已经达到这一点时,你唯一的理由就是退出。 You could reasonably schedule yourself to run again to make sure the app comes back, but a far better idea is to actually handle the exception at its source and prevent it from being an UnhandledException. 您可以合理地安排自己再次运行以确保应用程序返回,但更好的想法是在其源处实际处理异常并防止它成为UnhandledException。

In your app.config add the following element 在app.config中添加以下元素

<runtime>
  <!-- the following setting prevents the host from closing when an unhandled exception is thrown -->
  <legacyUnhandledExceptionPolicy enabled="1" />
</runtime>

You can, and you should use try...catch and handle the exceptions in every situation where an exception might occur. 您可以,并且应该使用try...catch并在可能发生异常的每种情况下处理异常。 (In languages like Java you can't even compile your code until you catch every exception that the called function might throw or declare explicitly that this particular function won't catch it, so the one that calls it should do it.) (在像Java这样的语言中,你甚至无法编译代码,直到你捕获被调用函数可能抛出的每个异常或明确声明这个特定函数不会捕获它,因此调用它的那个应该这样做。)

If you want to minimize the cases where you use try...catch some bad situations can be prevented with testing if the necessary conditions are met so you won't even call a function that is known for throwing a particular exception. 如果你想最小化你使用try...catch情况,如果满足必要的条件,可以通过测试防止一些不良情况,这样你甚至不会调用一个已知的抛出特定异常的函数。 Like calling 喜欢打电话

if (myByteArray != null)
{
    myIPAddress = new IPAddress(myByteArray);
}

because this way the IPAddress constructor won't throw an ArgumentNullException . 因为这样, IPAddress构造函数不会抛出ArgumentNullException

However in most of the cases this can't be done, especially with networking, because you can't predict if the cable will be cut, the signal will be lost, the database server will crush, etc. in the middle of the transaction. 然而,在大多数情况下,这是无法做到的,尤其是在网络方面,因为您无法预测电缆是否会被切断,信号是否会丢失,数据库服务器是否会在交易过程中粉碎等等。 So you should try...catch every time you connect to the network or send data to or receive data from a connection. 因此,每次连接到网络或向连接发送数据或从连接接收数据时,都应该try...catch

AppDomain.CurrentDomain.UnhandledException fires on any unhandled exception on any thread, but since CLR 2.0, the CLR forces application shutdown after your event handler completes. AppDomain.CurrentDomain.UnhandledException触发任何线程上的任何未处理的异常,但自CLR 2.0起,CLR会在事件处理程序完成后强制关闭应用程序。 However, you can prevent shutdown by adding the following to your application configuration file: 但是,您可以通过将以下内容添加到应用程序配置文件来阻止关闭:

<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="1" />
  </runtime>
</configuration>

我认为你需要使用Application.ThreadException当你订阅Application.ThreadException时 ,未处理的异常不会触及UnhandledException ,应用程序将继续运行。

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

相关问题 引发异常后任务是否继续运行? - Do tasks continue running after an exception is thrown? 如何告诉我的方法忽略异常并继续运行? - How can I tell my method to ignore an exception and continue running? 运行我的代码后,出现“未处理格式异常”错误 - I am getting a “Format Exception unhandled” error after running my code 如何在按钮onClick事件之后继续显示:内联? - How do I continue display:inline after a button onClick event? C#中发生异常后如何继续 - How to continue after exception occurred in C# 异常后自动继续 - Continue Automatically After Exception 如何处理用户代码未处理的索引超出范围异常? - How do I handle an index out of range exception unhandled by user code? 我该如何解决此错误:DotTeach.exe中发生了&#39;System.NullReferenceException&#39;类型的未处理异常 - how do i fix this error :An unhandled exception of type 'System.NullReferenceException' occurred in DotTeach.exe 发生未处理的异常后,我可以将执行返回到失败的方法吗? - Can I return execution to the failing method after an unhandled exception? 由于超时而出现异常后,我可以继续使用NetworkStream吗? - Can I continue using a NetworkStream after an exception due to a timeout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM