Using await in a catch or finally block – in C# 6.0

By | December 6, 2014

Using Async methods are very common now a days and while working with  async and await,  you may have experienced that you want to put some of the result awaiting either in a catch or finally block or in both. Consider a scenario, when there is an exception  and you want to log information into file or sending a service call to send exception details to server; they may take a long.   That case,  using await in an asynchronous method inside the catch block would be helpful; and this a new addition in C# 6.0

Similarly inside finally block, we can also using await to call any async method.

Following code snippet shows how this looks like.

public async Task SubmitDataToServer()
// Submit Data
await LogExceptionAsync();
await CloseConnectionAsync();

This is a very common scenarios to use await in catch and finally and would be very helpful for developer going forward.

Related Post : What’s new in C# 6.0


7 thoughts on “Using await in a catch or finally block – in C# 6.0

  1. Pingback: Conditional Exception Handling – Exception Filters in C# 6.0

  2. Pingback: Einstein Regarding Haters - The Daily Six Pack; Dec 9, 2014

  3. Bhagawat

    Very nice article… Thnaks for updating with new things

  4. Pingback: 10 New features of C# 6.0 that you should know | Abhijit's World of .NET

  5. Srinivasan

    I’ve a doubt in your post about “Keep tracking about Miscellaneous Files” option in solution explore of Visual Studio.
    If I enable this option in my project, whether this will be added / shown into my solution/project by default? I thought to comment this question in your post directly but there was no option to send the comment.

  6. Abhijit Jana Post author

    You should able to see that only after adding any files. You can refer the video as well.

    P.S: Comments are automatically disabled for the posts that are older than 100 days.

Comments are closed.