惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
DataBreaches.Net
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
Latest news
Latest news
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
G
Google Developers Blog
W
WeLiveSecurity
Project Zero
Project Zero
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
F
Full Disclosure
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
小众软件
小众软件
N
News | PayPal Newsroom
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
AI
AI
T
Tenable Blog
S
Schneier on Security
O
OpenAI News
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - Goodspeed

几种常见的函数 Caesar cipher 遗传算法之背包问题 Transport scheme NOT recognized: [stomp] error running git Canvas 旋转的图片 canvas时钟 火箭起飞 让图标转起来 Tomcat启动脚本 Parallel的陷阱 用Task代替TheadPool 使用ThreadPool代替Thread 正确停止线程 线程同步中使用信号量AutoResetEvent 异步和多线程的区别 C#和.NET Framework的关系 为什么泛型不支持协变性? 可空值类型与值类型这间的转换
Task中的异常处理
Goodspeed · 2014-11-02 · via 博客园 - Goodspeed

最简单的方式

    var t = new Task(() => {
                throw new Exception("unknow excption");
            });
            t.Start();
            try
            {
                t.Wait();
            }
            catch (AggregateException e)
            {
                foreach (var item in e.InnerExceptions)
                {
                    Console.WriteLine("异常类型\t{0}\n来自\t{1}\n异常内容\t{2}", item.GetType(), item.Source, item.Message);
                }
            }    

缺点:这个会阻塞当前线程。下面是改进版

var t = new Task(() => {
                throw new Exception("unknow excption");
            });
            t.Start();
            var cat = t.ContinueWith(task =>
            {
                foreach (var item in task.Exception.InnerExceptions)
                {
                    Console.WriteLine("异常类型\t{0}\n来自\t{1}\n异常内容\t{2}", item.GetType(), item.Source, item.Message);
                }
            }, TaskContinuationOptions.OnlyOnFaulted); //指定只应在延续任务前面的任务引发了未处理异常的情况下才安排延续任务
            Console.WriteLine("主线程退出");
            Console.ReadKey();

缺点:异常没有回到主线程。继续改进

var t = new Task(() => {
                throw new Exception("unknow excption");
            });
            t.Start();
            var cat = t.ContinueWith(task =>
            {
                throw task.Exception;
            }, TaskContinuationOptions.OnlyOnFaulted); //指定只应在延续任务前面的任务引发了未处理异常的情况下才安排延续任务
            Console.WriteLine("主线程退出");
            Thread.Sleep(1000);
            //异常处理
            try
            {
                cat.Wait();
            }
            catch (AggregateException e)
            {
                foreach (var item in e.InnerExceptions)
                {
                    Console.WriteLine("异常类型\t{0}\n来自\t{1}\n异常内容\t{2}", item.InnerException.GetType(), item.InnerException.Source, item.InnerException.Message);
                }
            }