问题描述
我想知道最好的方法是如果所有其他方法都失败了".
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.