C# 6.0 - Await in catch/finally

C# 6.0 以前 await 無法用在 catch/finally 區塊,C# 6.0 後開始支援。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Threading.Tasks;

namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Test();
System.Threading.Thread.Sleep(10000);
}

private static async void Test()
{
try
{
throw new Exception();
}
catch
{
Console.WriteLine("Catch...");
await Task.Delay(500);
}
finally
{
Console.WriteLine("Finally...");
await Task.Delay(500);
}
}
}
}