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

推荐订阅源

MyScale Blog
MyScale Blog
G
Google Developers Blog
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic
L
LINUX DO - 最新话题
V
Vulnerabilities – Threatpost
H
Hacker News: Front Page
T
Tor Project blog
P
Proofpoint News Feed
P
Privacy International News Feed
Recorded Future
Recorded Future
F
Fortinet All Blogs
量子位
博客园 - 聂微东
月光博客
月光博客
博客园 - Franky
SecWiki News
SecWiki News
G
GRAHAM CLULEY
腾讯CDC
Know Your Adversary
Know Your Adversary
宝玉的分享
宝玉的分享
The Cloudflare Blog
美团技术团队
小众软件
小众软件
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threatpost
爱范儿
爱范儿
A
Arctic Wolf
博客园 - 叶小钗
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Project Zero
Project Zero
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 三生石上(FineUI控件)
PCI Perspectives
PCI Perspectives
Latest news
Latest news
V
V2EX
罗磊的独立博客
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
S
Security Affairs
S
SegmentFault 最新的问题

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