TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Await in Catch Finally Blocks

C# Await in Catch Finally Blocks for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# await in catch/finally blocks

C# await is a keyword. It is used to suspend execution of the method until the awaited task is complete.

In C# 6.0, Microsoft added a new feature that allows us to use await inside the catch or finally block. So, we can perform asynchronous tasks while exception is occurred.

Let's see an example. Here, we are calling an asynchronous method inside the catch block.

C# await in Catch Block Example

using System;
using System.Threading.Tasks;
using System.IO;
using static System.Console;
namespace CSharpFeatures
{
    public class ExceptionAwait
    {
        public static void Main(string[] args)
        {           
            addAsync();
        }
        async static Task addAsync()
        {
            try
            {
                int[] arr = new int[5];
                arr[10] = 12;
            }
            catch(Exception e) {
                // Using await in catch block
                await ExceptionOccured();
            }
        }
        async static Task ExceptionOccured()
        {
           Console.WriteLine("Array Exception Occurred");
        } 
    }
}

Output

Array Exception Occurred

We can also use await inside the finally block. Let's see an example.

C# await in finally block example

using System;
using System.Threading.Tasks;
using System.IO;
using static System.Console;
namespace CSharpFeatures
{
    public class ExceptionAwait
    {
        public static void Main(string[] args)
        {
            addAsync();
        }
        async static Task addAsync()
        {
            try
            {
                int[] arr = new int[5];
                arr[10] = 12;
            }
            catch (Exception e)
            {
                // Using await in catch block
                await ExceptionOccured();
            }
            finally
            {
                // Using await in finally block
                await ReleasingResources();
            }
        }
        async static Task ExceptionOccured()
        {
            Console.WriteLine("Array Exception Occurred");
        }
        async static Task ReleasingResources()
        {
            Console.WriteLine("Resources has been released");
        }
    }
}

Output

Array Exception Occurred
Resources has been released





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf