Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why await is not allowed in a finally block?

Why await is not allowed in a finally block?

public async void Fn()
{
    try
    {
    }
    finally
    {
        await Task.Delay(4000);
    }
}

knowing that it is possible to get the Awaiter manually

public void Fn()
{
    try
    {
    }
    finally
    {
        var awaiter = Task.Delay(4000).GetAwaiter();
     }
}
like image 669
Sleiman Jneidi Avatar asked Feb 01 '13 12:02

Sleiman Jneidi


1 Answers

Taken from: Where can’t I use “await”?

Inside of a catch or finally block. You can use “await” inside of a try block, regardless of whether it has associated catch or finally blocks, but you can’t use it inside of the catch or finally blocks. Doing so would disrupt the semantics of CLR exception handling.

This is apparently no longer true in C# 6.0

Taken from: A C# 6.0 Language Preview

C# 6.0 does away with this deficiency, and now allows await calls within both catch and finally blocks (they were already supported in try blocks)

like image 158
BlakeH Avatar answered Oct 15 '22 21:10

BlakeH