Introduction
Welcome to the Asynchronous Programming series. In the previous three articles, we explained the async and await keywords and the return type of asynchronous methods and tasks. You can read them here.
- Asynchronous programming in C# 5.0: Part-1: Understand async and await
- Asynchronous Programming in C# 5.0 Part 2: Return Type of Asynchronous Method
- Asynchronous Programming in C# 5.0 Part 3: Understand Task in Asynchronous programming.
In this article, we will explain Exception Handling in asynchronous programming. I hope you are experienced with Exception Handling in C#, but you may not know how to implement Exception Handling in asynchronous programming. Let's see how to implement try-catch blocks in asynchronous programming. Have a look at the following code.
Traditional Try-Catch in Asynchronous programming
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Asynchronious
{
class Test
{
public Task ShowAsync()
{
return Task.Run(()=>{
Task.Delay(2000);
throw new Exception("My Own Exception");
});
}
public async void Call()
{
await ShowAsync();
}
}
class Program
{
public static void Main(String [] args)
{
Test t = new Test();
try
{
t.Call();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
We have declared a Test class with an asynchronous function, ShowAsync(), that will throw an exception. One more function (Call) will call the ShowAsync() function. From the Main() function we are calling the Call() function wrapping try catch blocks. We hope that in the catch block, the exception will be handled. Have a look at the following output:

Oh! The catch is not handling an exception? Why? The reason is that it's asynchronous in nature. As we know, in asynchronous programming, control does not wait for the function's result and it executes the next line. So when the function throws an exception, at that moment the program control is out of the try-catch block. This is why it exists.
Implement try-catch within the function
Let's implement a try-catch block within an asynchronous function. This is the solution to catch exceptions in asynchronous methods. Have a look at the following code. If you look closely inside the ShowAsync() function, then you will find we have implemented a try-catch within Task.run(). Within Task.run(), all processes are executed synchronously (in our example). So, if there is an exception, then it will be caught by the Exception Handling block.



Satish BhuktarPosted Jul 5, 2022, 2:20 AM
This is an excellent explanation in a short demo. Very Nice
binod khatriPosted Aug 30, 2021, 6:38 AM
Public async void Call() .. I have found in many articles that async void is just a crime.
Bohdan StupakPosted May 2, 2020, 9:03 AM
Looks like you've come great length to explain the fact that you're not awaiting your async functions. Although I recognize that it was really hard without calling .Result directly at the time of writing, nowadays it can be fixed with async Main method