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

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - ♂风车车

X509Certificate类解析证书的差异,算是.Net的BUG吗? Ninject 2.x细说---2.绑定和作用域 Ninject 2.x细说---1.基本使用 ASP.NET MVC3---1.入门 删除Windows 7系统保留分区100MB .Net并行编程系列文章导航 Entity Framework系列文章导航 多核时代 .NET Framework 4 中的并行编程9---线程安全集合类 多核时代 .NET Framework 4 中的并行编程8---任务的同步 多核时代 .NET Framework 4 中的并行编程7---任务工厂和任务计划 多核时代 .NET Framework 4 中的并行编程6---并行LINQ 多核时代 .NET Framework 4 中的并行编程5---并行循环Parallel Loop 多核时代 .NET Framework 4 中的并行编程3---任务并行库之Task (下) 多核时代 .NET Framework 4 中的并行编程2---任务并行库之Task (上) 多核时代 .NET Framework 4 中的并行编程1---概述 Entity Framework 4中删除所有数据行的几种方法 ADSL Modern+无线路由实现无线上网 ASP.Net4中实现自定义的请求验证 通过代码理解Asp.net4中的几种ClientIDMode设置.
多核时代 .NET Framework 4 中的并行编程4---异常处理
♂风车车 · 2011-08-24 · via 博客园 - ♂风车车

任何程序都避免异常情况发生.那么如何在发生异常情况时,很好的处理掉异常,以便是我们的程序继续良好的运行呢?那么介绍几种处理.Net中并行编程处理异常的几种方式.

方法1: AggregateException

表示在应用程序执行期间发生的一个或多个错误.

当使用某个静态或实例任务类Task的Wait(),WaitAll(),WaitAny()方法和Result属性时,会传播异常,您可通过将调用包括在 try-catch 语句中来处理这些异常。 如果任务是所附加子任务的父级,或者您在等待多个任务,则可能会引发多个异常。 为了将所有异常传播回调用线程,任务基础结构会将这些异常包装在 AggregateException 实例中。 AggregateException 具有一个 InnerExceptions 属性,可枚举该属性来检查引发的所有原始异常,并单独处理(或不处理)每个异常。 即使只引发了一个异常,也仍会将该异常包装在 AggregateException 中。例子如下:

        static void DealException()

        {

            Task task1 = new Task(() =>

            {

                throw new NotImplementedException("没有实现");

            });

            Task task2 = new Task(() =>

            {

                throw new ArgumentNullException("参数空");

           });

            Task task3 = new Task(() =>

            {

                throw new Exception("异常");

            });

            Task task4 = new Task(() =>

            {

                Console.WriteLine("正常");

            });

            task1.Start();

            task2.Start();

            task3.Start();

            task4.Start();

            try

            {

                Task.WaitAll(task1, task2, task3, task4);

           }

            catch (AggregateException agex)

            {

                foreach (var ex in agex.InnerExceptions)

                {

                    Console.WriteLine("异常内容是:{0}", ex.Message);

                }

              });

            }

通过代码,我们可以看到,我们只需要使用try{ …}catch()来捕获AggregateException异常,然后去处理其包含的异常即可.

方法2: AggregateException的Handle来处理每个异常.

AggregateException类型提供了一个Handle调用处理程序,以便我们对每个任务异常进行处理.代码如下:

static void DealException()

        {

           Task task1 = new Task(() =>

            {

                throw new NotImplementedException("没有实现");

            });

            Task task2 = new Task(() =>

            {

                throw new ArgumentNullException("参数空");

           });

            Task task3 = new Task(() =>

            {

                throw new Exception("异常");

            });

            Task task4 = new Task(() =>

            {

                Console.WriteLine("正常");

            });

            task1.Start();

            task2.Start();

            task3.Start();

            task4.Start();

            try

            {

                Task.WaitAll(task1, task2, task3, task4);

           }

            catch (AggregateException agex)

            {

                agex.Flatten().Handle((ex) =>

                {

                    if (ex is NotImplementedException)

                    {

                        Console.WriteLine("忽略");

                        return true;

                    }

                    else

                    {

                        Console.WriteLine("异常内容是:{0},已处理", ex.Message);

                        return true;

                    }

                 });

            }

            Console.ReadLine();

        }

上面代码中,捕获到异常,通过AggregateException异常实例的Flatten() 方法移除所有嵌套的 AggregateExceptions。处理程序返回true表示异常被处理。返回false则表示异常未被处理。

方法3:TaskScheduler.UnobservedTaskException 事件

当出错的 Task 的未观察到的异常将要触发异常升级策略时发生,默认情况下,这将终止进程。它提供了最后一种手段处理所有未处理异常。通过处理这个事件,就不用终止应用程序,而用你自己的异常处理逻辑替代它。代码如下:

   TaskScheduler.UnobservedTaskException +=

 (object sender, UnobservedTaskExceptionEventArgs eventArgs) =>

 {

     eventArgs.SetObserved();

    eventArgs.Exception.Flatten().Handle(ex =>

     {

         Console.WriteLine("异常是:{0}", ex.Message);

         return true;

     });

 };

            Task task1 = new Task(() =>

            {

                throw new NullReferenceException();

            });

            Task task2 = new Task(() =>

            {

                throw new ArgumentOutOfRangeException();

            });

task1.Start(); task2.Start();

            while (!task1.IsCompleted || !task2.IsCompleted)

            {

                Thread.Sleep(500);

            }

            Console.ReadLine();

        }

好,异常的处理介绍完毕。