Let say I have an async method:
public async Task Do()
{
await Task.Delay(1000);
}
Another method is trying to call Do method inside catch block
public async Task DoMore()
{
try
{
}
catch (Exception)
{
await Do(); //compiled error.
}
}
But this way, the compiler does not allow using await inside catch, is there any reason behind the scene why we could not use it that way?
Update
This will be supported in C# 6. It turned out that it wasn’t fundamentally impossible, and the team worked out how to do so without going mad in the implementation 🙂
Original answer
I strongly suspect it’s the same reasoning that prevents
yield returnfrom being used in a catch block.In particular:
Replace "yield" with "await" there, and I think you’ll have your answer.
It feels like it would be an odd thing to want to do in most cases, and you should usually be able to rewrite your code fairly easily to await after the
catchblock – unless you were trying to await something and then throw, of course. In that case it would be a bit of a pain, I admit…