A task with built-in exception handling?

When using Jobs, you need to be especially careful when handling exceptions, here is an example:

class Program
{
    static void Main(string[] args)
    {
        Task<int> task = new Task<int>(Test);
        task.ContinueWith(ExceptionHandler, TaskContinuationOptions.OnlyOnFaulted);
        task.Start();
        Console.ReadLine();
    }

    static int Test()
    {
        throw new Exception();
    }

    static void ExceptionHandler(Task<int> task)
    {
        var exception = task.Exception;
        Console.WriteLine(exception);
    }
}

From here

My question is whether there is a way to create a custom task that will handle (register for maintenance) all exceptions without manually specifying it by the developer. Some kind of legacy of help.

+4
source share
1 answer

You can create MyLoggingTaska Task as a subclass by implementing the constructors appropriate for you, or all of them should be safe (I see 8 of them in the .NET Framework 4.5) and override Start ().

MyLoggingTask + , "" using Task = Your.NameSpace.MyLoggingTask, , .

public static StartWithLogging<T>(this Task<T> task), ContinueWith. Start() StartWithLogging() + .

, , " ".

0

All Articles