简体   繁体   中英

How to catch Task exception in C#

I would like to catch all Task's exceptions of a batch of task, but I don't find the best solution. Maybe anyone can help me please?

I have a function with the following declaration:

public async Task CreateBooking(CreateBookingModel createBookingModel)

In this method a throw exception can be do

If I make a foreach like that:

foreach (DateTime day in EachDay(originalStartDate, originalEndDate))
{
    createBookingModel.StartDate = day;
    createBookingModel.EndDate = day;

    try
    {
        CreateBooking(createBookingModel);
    }
    catch (Exception ex)
    {
       raiseToTeams(ex, "Creation error");
    }
}

I don't received any else if the creation throw an exception. I tried to make that:

List<Task> tasks = new List<>();
foreach (DateTime day in EachDay(originalStartDate, originalEndDate))
{
    createBookingModel.StartDate = day;
    createBookingModel.EndDate = day;
    tasks.Add(CreateBooking(createBookingModel));              
}

try
{
    Task.WaitAll(tasks.toArray());
}
catch(AggregateException ex)
{
    ex.InnerExceptions.ForEach(subExp =>
    {
     if (subExp is ExternalBookingException)
     {
       raiseToTeams(subExp, "Creation error");
     }
    });
}

But I have 2 problems, if only one exception occurs no problem I catch it in the aggregation, if more that one I catch nothing even on a Exception and not a simple Aggregate catch. And if each Task added before WaitAll() is finish, the line block infinitly, that I don't want !

Anybody have a solution plz?

Note, that

CreateBooking(createBookingModel);

just starts the Task and returns it. In order to get Task result you should await it:

try
{
    // await - start task and await for its completion
    await CreateBooking(createBookingModel);
}
catch (Exception ex)
{
    raiseToTeams(ex, "Creation error");
}

Same if you have several tasks to complete:

List<Task> tasks = new List<>();

foreach (DateTime day in EachDay(originalStartDate, originalEndDate))
{
    createBookingModel.StartDate = day;
    createBookingModel.EndDate = day;
    tasks.Add(CreateBooking(createBookingModel));              
}

try
{
    // Note WhenAll instead of WaitAll
    // Note await
    await Task.WhenAll(tasks);
}
catch (Exception ex) // <- Note plain exception, not AggregatedException
{
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM