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

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

问题描述

我想知道如果一切都失败了就抓住它"的最佳方法是什么.

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

我的意思是,您在应用程序中处理尽可能多的异常,但仍然肯定会有错误,所以我需要一些东西捕获所有未处理的异常,以便我可以收集信息并存储将它们保存在数据库中或将它们提交给网络服务.

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();
    }
}

P.S. 请在更高级别处理未处理的 Application.ThreadException (WinForms) 或 DispatcherUnhandledException (WPF).

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

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

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