.NET - What#39;s the best way to implement a quot;catch all exceptions handlerquot;(.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 - 实现“捕获所有异常处理程序"的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.NET - 实现“捕获所有异常处理程序"的最佳方
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01