Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

ASYNC IN A CATCH AND FINALLY BLOCK


ASYNC IN A CATCH AND FINALLY BLOCK:

In C# 5.0 we can use try, catch and finally blocks together but was unable to use async and await with catch {} and finally {} blocks due to implementation specific issues in the C# compiler. 

This is nice feature provided by C# 6.0, you can do some task without blocking current thread.

Requirement is that create some console application as background process that runs every day and updates data, we require to maintain logs and create notification for exception or failure in that case this new feature is use full.

Example(only exception code in sample example):

      catch (SqlException ex)
            {
                const string errormsg = "Sql exception encountered.";
                await Task.Delay(3000); //waits for 3 seconds
                logger.WriteError(errormsg, ex);
                SendErrorEmail(errormsg, ErrorEmailSubject);
 
            }
            catch (NullReferenceException ex)
            {
               const string errormsg = "Null reference exception encountered.";
                await
                logger.WriteError(errormsg, ex);
                SendErrorEmail(errormsg, ErrorEmailSubject);
            }
            catch (ArgumentNullException ex)
            {
                const string errormsg = “Argument null reference exception encountered.";
                await Task.Delay(2000); //waits for 2 seconds
                logger.WriteError(errormsg, ex);
                SendErrorEmail(errormsg, ErrorEmailSubject);
            }
            finally
            {
                //here release all resources
            }

 < > 

Invalid entry,please enter valid data.

Loading