.NET - 实现“捕获所有异常处理程序"的最佳方

.NET - What#39;s the best way to implement a quot;catch all exceptions handlerquot;(.NET - 实现“捕获所有异常处理程序的最佳方式是什么?)
本文介绍了.NET - 实现“捕获所有异常处理程序"的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道最好的方法是如果所有其他方法都失败了".

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

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 - 实现“捕获所有异常处理程序"的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)