如何捕获C#中异步编程Task中的异常

在网上看到很多关于如何捕获异步编程中的异常的方法,有很多写的都不是很明确,而且很多写的方法都不正确。异步编程中的异常捕获和同步编程的异常捕获其实不太一样,但也非常简单。

使用WhenAll执行任务,代码如下:

  public async Task Start()
        {
            Task taskResult = null;
            Task t1 = null;
            Task t2 = null;
            Task t3 = null;
            try
            {

                t1 = GetData1();
                t2 = GetData2();
                t3 = t1.ContinueWith((rst) =>
                {
                    //Console.WriteLine("hahaha!" + rst.Result);
                    throw new Exception("Exception3");
                });
                await (taskResult = Task.WhenAll(t1, t2,t3));
                Console.WriteLine("Complate1!");
                Console.WriteLine("t1:" + t1.Result + ";t2:" + t2.Result);
                Console.WriteLine("Complate2!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (taskResult != null && taskResult.IsFaulted)
                {
                    foreach (var oneEx in taskResult.Exception.InnerExceptions)
                    {
                        Console.WriteLine(oneEx.Message);
                    }
                }
           
             }
        }

        private async Task GetData1()
        {
            await Task.Delay(1000);
            Console.WriteLine("调用GetData1");
            await GetData4();
            throw new Exception("Exception1");
            //return "123";
        }

        private async Task GetData2()
        {
            await Task.Delay(2000);
            Console.WriteLine("调用GetData2");
            throw new Exception("Exception2");
            //return "456";
        }


        private async Task GetData4()
        {
            await Task.Delay(1000);
            Console.WriteLine("调用GetData4");
            throw new Exception("Exception4");
            //return "123";
        }

使用 WaitAll执行方法时,代码如下:

  public async Task Start()
        {
            Task t1 = null;
            Task t2 = null;
            Task t3 = null;
            try
            {

                t1 = GetData1();
                t2 = GetData2();
                t3 = t1.ContinueWith((rst) =>
                {
                    //Console.WriteLine("hahaha!" + rst.Result);
                    throw new Exception("Exception3");
                });
                Task.WaitAll(t1, t2, t3);
                Console.WriteLine("Complate1!");
                Console.WriteLine("t1:" + t1.Result + ";t2:" + t2.Result);
                Console.WriteLine("Complate2!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (t1 != null && t1.IsFaulted)
                {
                    foreach (var oneEx in t1.Exception.InnerExceptions)
                    {
                        Console.WriteLine(oneEx.Message);
                    }
                }
                if (t2 != null && t2.IsFaulted)
                {
                    foreach (var oneEx in t2.Exception.InnerExceptions)
                    {
                        Console.WriteLine(oneEx.Message);
                    }
                }
                if (t3 != null && t3.IsFaulted)
                {
                    foreach (var oneEx in t3.Exception.InnerExceptions)
                    {
                        Console.WriteLine(oneEx.Message);
                    }
                }


            }
        }

你可能感兴趣的:(C#,编程技巧,c#,开发语言,后端)