1

I'm having this code snippet:

class Program
{
    public static async Task ProcessAsync(string s)
    {
        Console.WriteLine("call function");
        if (s == null)
        {
            Console.WriteLine("throw");
            throw new ArgumentNullException("s");
        }
        Console.WriteLine("print");
        await Task.Run(() => Console.WriteLine(s));
        Console.WriteLine("end");
    }
    public static void Main(string[] args)
    {
        try
        {
            ProcessAsync(null);
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

It runs and prints:

call function
throw

Ok, and exception is thrown, but the main function's try/catch is not able to catch the exception, if I remove the try/catch, main doesn't report unhandled exception either. This is very weird, I googled and it says there's trap in [await] but doesn't explain how and why.

So my question, why here the exception is not caught, what's the pitfalls of using await?

Thanks a lot.

Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • 4
    Well, you `Run` the `Task` but you do it in *fire and forget* manner: you don't `await` for the execution results; so if an exception is thrown it will be within the task only. Others there is a problem: in which thread (and when) it should be re-thrown? – Dmitry Bychenko Mar 06 '20 at 09:12
  • 4
    Change your `Main()` method to `public static async Task Main(string[] args)` and `await` your call to `ProcessAsync(null);` – Matthew Watson Mar 06 '20 at 09:14
  • See also [here](https://stackoverflow.com/questions/5383310/catch-an-exception-thrown-by-an-async-void-method) and [here](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming) for more information – Stefano Branco Mar 06 '20 at 09:15
  • 1
    https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskscheduler.unobservedtaskexception?view=netframework-4.8 – Dmitry Bychenko Mar 06 '20 at 09:17
  • 1
    https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/exception-handling-task-parallel-library?redirectedfrom=MSDN – faithfull Mar 06 '20 at 09:18

1 Answers1

5

Within an async method, any exceptions are caught by the runtime and placed on the returned Task. If your code ignores the Task returned by an async method, then it will not observe those exceptions. Most tasks should be awaited at some point to observe their results (including exceptions).

The easiest solution is to make your Main asynchronous:

public static async Task Main(string[] args)
{
  try
  {
    await ProcessAsync(null);
  }
  catch(Exception e)
  {
    Console.WriteLine(e.Message);
  }
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810