2013-02-18 220 views
1

我们已经开发了一个.NET 3.5 CF应用程序,由于未处理的异常,我们遇到了一些应用程序崩溃,引发了一些lib代码。如何“捕获”未处理的异常

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

有没有办法捕获所有未处理的异常?或者至少,从消息框中获取文本。我们的大多数客户只需重新启动设备,以便我们无法查看异常消息框。

任何想法?

回答

6

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

[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); 
} 
+0

令人惊叹!我将它添加到了我的一些项目中,但没有显示任何错误迹象,并且这种情况正在捕获我从未知道发生的异常。 – jp2code 2013-02-25 02:45:52

0

我做了类似于ctacke所做的一些事情。

private static Form1 objForm; 

[MTAThread] 
static void Main(string[] args) 
{ 
    objForm = new Form1(); 
    try 
    { 
    Application.Run(objForm); 
    } catch (Exception err) { 
    // do something with the info here - log to a file or whatever 
    MessageBox.Show(err.Message); 
    if ((objForm != null) && !objForm.IsDisposed) 
    { 
     // do some clean-up of your code 
     // (i.e. enable MS_SIPBUTTON) before application exits. 
    } 
    } 
} 

也许他可以评论我的技术是好还是坏。

+2

这将经常错过工作线程和本机异常中的异常。一个AppDomain.UnhandledException处理程序往往会捕获更多的东西。 – ctacke 2013-02-19 15:08:28