Task异常处理
下面有两个例子代码,可以直接复制粘贴到.net core中运行。两个代码要实现的功能完全一样,但是内核却又很大差异。
先看下面用await的例子与输出:
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
System.Console.WriteLine($"Main Task ID:{Thread.CurrentThread.ManagedThreadId}");
var task = Task.Run(() =>
{
System.Console.WriteLine($"In Task.Run(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
int[] vary=new int[5];
while (true)
{
Thread.Sleep(3000);
int d = vary[6];
}
});
// Just continue on this thread, or await with try-catch:
try
{
await task;
}
catch (IndexOutOfRangeException ex)
{
System.Console.WriteLine(ex.Message);
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
catch(AggregateException ex)
{
System.Console.WriteLine(ex.Message);
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
finally
{
//...
}
System.Console.WriteLine("Reach end.");
Console.ReadKey();
}
}
/*
Main Task ID:1
In Task.Run(), Task ID:4
Index was outside the bounds of the array.
Catch System.IndexOutOfRangeException
After Wait(), Task ID:4
Reach end.
*/
再看Task.Wait()方法下的异常处理与输出:
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main()
{
System.Console.WriteLine($"Main Task ID:{Thread.CurrentThread.ManagedThreadId}");
var task = Task.Run(() =>
{
System.Console.WriteLine($"In Task.Run(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
int[] vary=new int[5];
while (true)
{
Thread.Sleep(3000);
int d = vary[6];
}
});
// Just continue on this thread, or await with try-catch:
try
{
task.Wait();
}
catch (IndexOutOfRangeException ex)
{
System.Console.WriteLine($"Catch {ex.GetType()}");
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
catch(AggregateException ex)
{
System.Console.WriteLine($"Catch {ex.GetType()}");
System.Console.WriteLine($"After Wait(), Task ID:{Thread.CurrentThread.ManagedThreadId}");
}
finally
{
//...
}
System.Console.WriteLine("Reach end.");
Console.ReadKey();
}
}
/*
Main Task ID:1
In Task.Run(), Task ID:4
Catch System.AggregateException
One or more errors occurred. (Index was outside the bounds of the array.)
After Wait(), Task ID:1
Reach end.
*/
从例子中可以看出,await之后的代码其实都是在新的线程(4线程)中执行,而Task.Wait()方法后的线程则是在主线程中执行。因此,await之后的代码完全以传统方式处理异常;而Task.Wait()抛出的异常则由于是从新线程往外部线程抛出,所以它是被重新封装为AggregateException异常抛出。