C # 6.0: Using await in catch and finally

Source: Internet
Author: User

The Asyn method is a very common method that you might have experienced when using async and await, that is, you want to use them in catch blocks or finally blocks, For example, when a exception appears and you want to write the log in a file or call a service to send exception information to the server, these operations can be time-consuming. In this case, using await in the Async method in the Catch block will be helpful. This has been added to C # 6.0.

Also in the finally block, we can use await to invoke the Async method.

The following code block shows how to use them.

 Public Async Task Submitdatatoserver () {    try    {        //  Submit Data     }     Catch     {        await  logexceptionasync ();    }     finally     {        await  closeconnectionasync ();    }}    
View Code

This is a common scenario for using await in catch blocks and finally blocks, and this feature will bring great convenience to developers.

C # 6.0: Using await in catch and finally

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.