如何“赶上”未处理的异常 [英] How to "catch" unhandled Exceptions

查看:70
本文介绍了如何“赶上”未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经开发了一个.NET 3.5 CF应用程序,由于未处理的异常,某些lib代码引发了一些应用程序崩溃。

We've developed a .NET 3.5 CF Application and we're experiencing some application crashes due to unhandled exceptions, thrown in some lib code.

该应用程序终止并显示标准应用程序弹出异常消息框。

The application terminates and the standard application popup exception message box is shown.

是否可以捕获所有未处理的异常?或者至少,从消息框中捕获文本。我们大多数的客户只是简单地重启设备,以至于我们无法查看异常消息框。

Is there a way to catch all unhandled exceptions? Or at least, catch the text from the message box. Most of our customers simply restart the device, so that we're not able to have a look on the exception message box.

有什么想法吗?

推荐答案

您是否添加了 UnhandledException 事件处理程序?

Have you added an UnhandledException event handler?

[MTAThread]
static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

    // start your app logic, etc
    ...
}

static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    var exception = (Exception)e.ExceptionObject;

    // do something with the info here - log to a file or whatever
    MessageBox.Show(exception.Message);
}

这篇关于如何“赶上”未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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