栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

.NET-实现“捕获所有异常处理程序”的最佳方法是什么

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.NET-实现“捕获所有异常处理程序”的最佳方法是什么

我刚刚玩过AppDomain的UnhandledException行为,(这是未处理的异常注册到的最后阶段)

是的,处理完事件处理程序后,您的应用程序将终止,并显示讨厌的“ …程序停止工作对话框”。

:)您 仍然 可以避免这种情况。

查看:

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)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495420.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号