A good solution for await in try/catch/finally?

后端 未结 4 1720
南笙
南笙 2020-12-04 07:20

I need to call an async method in a catch block before throwing again the exception (with its stack trace) like this :

try
{
    //         


        
4条回答
  •  旧巷少年郎
    2020-12-04 07:38

    You should know that since C# 6.0, it's possible to use await in catch and finally blocks, so you could in fact do this:

    try
    {
        // Do something
    }
    catch (Exception ex)
    {
        await DoCleanupAsync();
        throw;
    }
    

    The new C# 6.0 features, including the one I just mentioned are listed here or as a video here.

提交回复
热议问题