.NET - 什么是实现&QUOT的最佳方式;捕获所有异常处理程序和QUOT; [英] .NET - What's the best way to implement a "catch all exceptions handler"

查看:198
本文介绍了.NET - 什么是实现&QUOT的最佳方式;捕获所有异常处理程序和QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道最好的办法是有一个如果一切都失败抓住它是什么。

I'm wondering what the best way is to have a "if all else fails catch it".

我的意思是,你正在处理尽可能多的例外地在你的应用程序, 但还是有必然是错误的,所以我需要有一些 捕捉所有未处理的异常,所以我可以收集信息并存储 他们在一个数据库或将其提交给Web服务。

I mean, you're handling as much exceptions as possible in your application, but still there are bound to be bugs, so I need to have something that catches all unhandled exceptions so I can collect information and store them in a database or submit them to a web service.

是否AppDomain.CurrentDomain.UnhandledException事件捕捉一切? 即使该应用程序是多线程的?

Does the AppDomain.CurrentDomain.UnhandledException event capture everything? Even if the application is multithreaded?

边注:Windows Vista的公开本机API函数,允许任何应用程序 到崩溃后恢复本身......想不到这个名字现在......但我宁愿不要 使用它,因为我们的许多用户仍在使用Windows XP。

Side note: Windows Vista exposes native API functions that allow any application to recover itself after a crash... can't think of the name now... but I'd rather not use it, as many of our users are still using Windows XP.

推荐答案

我刚玩的AppDomain的UnhandledException行为, (这是未处理的异常被记录在最后阶段)

I have just played with AppDomain's UnhandledException behavior, (this is the last stage the unhandled exception is registered at)

是的,处理事件处理程序的应用程序将被终止,讨厌在......节目停止工作对话框中所示。

Yes, after processing the event handlers your application will be terminated and the nasty "... program stopped working dialog" shown.

:) 您的还是的可避免这种情况。

:) You still can avoid that.

查看:

class Program
{
    void Run()
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

        Console.WriteLine("Press enter to exit.");

        do
        {
            (new Thread(delegate()
            {
                throw new ArgumentException("ha-ha");
            })).Start();

        } while (Console.ReadLine().Trim().ToLowerInvariant() == "x");


        Console.WriteLine("last good-bye");
    }

    int r = 0;

    void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Interlocked.Increment(ref r);
        Console.WriteLine("handled. {0}", r);
        Console.WriteLine("Terminating " + e.IsTerminating.ToString());

        Thread.CurrentThread.IsBackground = true;
        Thread.CurrentThread.Name = "Dead thread";            

        while (true)
            Thread.Sleep(TimeSpan.FromHours(1));
        //Process.GetCurrentProcess().Kill();
    }

    static void Main(string[] args)
    {
        Console.WriteLine("...");
        (new Program()).Run();
    }
}

PS 请上级处理未处理的Application.ThreadException(的WinForms)或DispatcherUnhandledException(WPF)。

P.S. Do handle the unhandled for Application.ThreadException (WinForms) or DispatcherUnhandledException (WPF) at the higher level.

这篇关于.NET - 什么是实现&QUOT的最佳方式;捕获所有异常处理程序和QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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