zoukankan      html  css  js  c++  java
  • TPL中的异常

    1、TPL中,如果程序中出现异常,除非使用try...catch进行捕获异常,否则有呢能会感觉不到出现了异常。

    2、TPL程序有时候还会抛出AggregateException,这通常发生在并行有多个任务执行的情况下,因为多个并行的任务可能会发生多个异常,因此会包装为AggregateException异常,AggregateException的InnerExceptions属性可以获得多个异常对象的信息,看下面的代码:

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            HttpClient hc = new HttpClient();
            // 存在的url地址
            string url1 = "http://www.baidu.com";
            // 不存在的url地址
            string url2 = "http://www.121qwqwqw.com";
            string url3 = "http://www.7546456dfsdf.com";
            var task1 = hc.GetStringAsync(url1);
            var task2 = hc.GetStringAsync(url2);
            var task3 = hc.GetStringAsync(url3);
            // 并行执行三个任务
            Task.WaitAll(task1, task2, task3);
            MessageBox.Show("下载成功");
           
        }
        catch (AggregateException ae)
        {
            // 循环遍历输出异常信息
            foreach (var item in ae.InnerExceptions)
            {
                MessageBox.Show(item.Message);
            }
        }
    }

    运行程序的时候,就会循环输出多个异常信息。

  • 相关阅读:
    odoo10 入门
    git 命令详细介绍
    odoo中Python实现小写金额转换为大写金额
    {DARK CTF } OSINT/Eye
    2020 12 18
    2020 12 17
    2020 12 16
    2020 12 15
    2020 11 14
    2020 11 13
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/12344237.html
Copyright © 2011-2022 走看看