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

推荐订阅源

T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy & Cybersecurity Law Blog
S
Schneier on Security
PCI Perspectives
PCI Perspectives
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Troy Hunt's Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
Know Your Adversary
Know Your Adversary
F
Fortinet All Blogs
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
Jina AI
Jina AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Vulnerabilities – Threatpost
P
Privacy International News Feed
T
Tor Project blog
S
Security Affairs
S
Securelist
F
Full Disclosure
D
Docker
酷 壳 – CoolShell
酷 壳 – CoolShell
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
Apple Machine Learning Research
Apple Machine Learning Research
N
News and Events Feed by Topic
Recorded Future
Recorded Future
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
Stack Overflow Blog
Stack Overflow Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
I
Intezer
Security Latest
Security Latest
Scott Helme
Scott Helme
U
Unit 42
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog

博客园 - 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);
                }
            }