Where to handle task exceptions

I perform several I / O cycles in separate tasks. These loops may occur with exceptions. If an exception occurs, I want to alert the caller so that he can:

  • register it
  • kill all the threads IO
  • reset connection
  • restart input / output streams

The user interface should remain responsive. What is the preferred method for handling this scenario? The following is an illustrative program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskExceptionCatching
{
    class Program
    {
        static void Main(string[] args)
        {
            startLoops();

            System.Console.WriteLine("Type 'Exit' when you're ready to stop.");
            while (System.Console.ReadLine() != "Exit")
            {
                System.Console.WriteLine("Seriously, just type 'Exit' when you're ready to stop.");
            }
        }

        static private void startLoops()
        {
            System.Console.WriteLine("Starting fizzLoop.");
            var fizzTask = Task.Factory.StartNew(new Action(fizzLoop));

            System.Console.WriteLine("Starting buzzLoop.");
            var buzzTask = Task.Factory.StartNew(new Action(buzzLoop));
        }

        static private void fizzLoop()
        {
            while (true)
            {
                //simulate something error prone, like some risky IO
                System.Threading.Thread.Sleep(200);
                bool isErr = (new Random().Next(1, 100) == 10);
                if (isErr)
                    throw new Exception("Fizz got an exception.");
            }
        }

        static private void buzzLoop()
        {
            while (true)
            {
                //simulate something error prone, like some risky IO
                System.Threading.Thread.Sleep(200);
                bool isErr = (new Random().Next(1, 100) == 10);
                if (isErr)
                    throw new Exception("Buzz got an exception.");
            }
        }
    }
}
+4
source share
2 answers

This may be one of the rare cases when a method async voidmay be convenient:

static async void StartAndMonitorAsync(Func<Task> taskFunc)
{
    while (true)
    {
        var task = taskFunc();
        try
        {
            await task;
            // process the result if needed
            return;
        }
        catch (Exception ex)
        {
            // log the error
            System.Console.WriteLine("Error: {0}, restarting...", ex.Message);
        }
        // do other stuff before restarting (if any)
    }
}

static private void startLoops()
{
    System.Console.WriteLine("Starting fizzLoop.");
    StartAndMonitorAsync(() => Task.Factory.StartNew(new Action(fizzLoop)));

    System.Console.WriteLine("Starting buzzLoop.");
    StartAndMonitorAsync(() => Task.Factory.StartNew(new Action(buzzLoop)));
}

async/await, Task.ContinueWith.

startLoops ( " " ), StartAndMonitorAsync , , CancelltionToken ( " " .

+1

MSDN " Task AggregateException.Exception InnerExceptions, , , , ( ) . , - AggregateException."

try
{
System.Console.WriteLine("Starting fizzLoop.");
            var fizzTask = Task.Factory.StartNew(new Action(fizzLoop));

            System.Console.WriteLine("Starting buzzLoop.");
            var buzzTask = Task.Factory.StartNew(new Action(buzzLoop));

}
catch (AggregateException ae)
{
    // Assume we know what going on with this particular exception. 
    // Rethrow anything else. AggregateException.Handle provides 
    // another way to express this. See later example. 
    foreach (var e in ae.InnerExceptions)
    {
        if (e is MyCustomException)
        {
            Console.WriteLine(e.Message);
        }
        else
        {
            throw;
        }
    }

}
0

Source: https://habr.com/ru/post/1540778/


All Articles