Using await operator in catch and finally blocks
Finally, in C# 6.0, you can now use the await
keyword in the catch
and finally
blocks. Previously, developers had to resort to all sorts of strange workarounds to achieve what is now easily achievable in C# 6.0. There really is not much more to it than the following.
Getting ready
We will create another class that will simulate the deletion of a file. An exception is thrown, and the catch
block is then executed along with the finally
statement. In both the catch
and finally
clauses, we will delay and await a task for 3 seconds. Then, we will output this delay to the console application window.
How to do it…
- Create a class called
Recipe9AwaitInCatchFinally
and add a method calledFileRunAsync()
to the ...
Get C# Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.