Up until now, you couldn’t put an await in a catch or finally block. That changes in C# 6.
I’ve got this Logger class, whose implementation is pretty trivial. This is how I’m using it in a simple WPF application’s button event handler:
private async void Button_Click(object sender, RoutedEventArgs e)
{
using (var logger = new Logger())
{
try
{
await logger.LogAsync("Executing operation...");
}
catch (Exception ex)
{
await logger.LogAsync(ex.ToString());
}
finally
{
await logger.FlushAsync();
}
}
}
As you can see, in C# 6 you can now await asynchronous operations from within your catch and finally blocks, which is very useful in cases such as the above.