0

I've argued with my colleague about handling exceptions in Task.Run blocks.

For example, I have this code:

private static async Task MainAsync()
{
    try
    {
        await Task.Run(() => throw new Exception());
    }
    catch (Exception)
    {
        Console.WriteLine("oops");
    }
}

If I run this code (in debug mode), I'll get the message from vs2019 about the unhandled exception. But if I press the continue button, the app will work correctly and the exception will be handled.

Is it the correct way to catch exceptions from Task.Run?

Noisy88
  • 181
  • 1
  • 14
  • 1
    Does this answer your question? [How to handle Task.Run Exception](https://stackoverflow.com/questions/32067034/how-to-handle-task-run-exception) – Arli Chokoev Dec 30 '20 at 13:55
  • 2
    It is fine. Alternatively, catch them inside the `Task.Run` itself (so they don't bubble out). Depends what you want to do really. – mjwills Dec 30 '20 at 13:56
  • 1
    As often, it depends. In your example, the exception is swallowed. The caller of MainAsync can't know if the execution is successful. In some case, this information is needed, by example to start the next step. – vernou Dec 30 '20 at 13:56
  • @Arli Chokoev did you see there any mentions of async/await ? It's not a similar case! – Noisy88 Dec 30 '20 at 14:03
  • @mjwills instead of "throw new Exception()" I have the calling of some method of external DLL library which is able to throw exceptions. I know that I can wrap this block of code in a try-catch block, but I need to get the exception to process it (to get an error code). If this async block throws an exception, the main block of try-catch will be able to catch and process this exception. – Noisy88 Dec 30 '20 at 14:08
  • What do you intend to do with the exceptions, when they occur? Is it OK to just write them in the `Console`, and then forget about them? – Theodor Zoulias Dec 30 '20 at 15:02
  • 1
    `I've argued with my colleague about handling exceptions in Task.Run blocks.` What approach is your colleague suggesting? It feels like you have shown one side of the argument but not the other... – mjwills Dec 30 '20 at 22:13
  • @mjwills my colleague told me that way might be dangerous because of the unhandled exception. He got the message from VS2019 "exception not handled by user code" in this block of code. I've found the article in msdn about this message (see the note): https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/exception-handling-task-parallel-library "This error is benign" - as I told him, the application won't crash if this way of exception handling is applied. – Noisy88 Dec 31 '20 at 08:04
  • `In a meaningful application` as per that link @Noisy88 shows the approach you are using is fine. – mjwills Dec 31 '20 at 08:34

1 Answers1

2

Generally, exception in tasks (objects of class Task) are placed on tasks, i.e. if some code in a task throws exception inside that task - it is stored in Exception property in Task class.

await operator automatically "unpacks" exception and throws. So your way is totally correct, but I think stack trace might change - so you need to keep that in mind.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • Stack trace doesn't matter. I only need to get the error code from that exception. It works perfectly. – Noisy88 Dec 30 '20 at 14:10
  • 1
    There is the explanation of this case: https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/exception-handling-task-parallel-library – Noisy88 Dec 31 '20 at 08:08