简体   繁体   中英

Exception catched as System.AggregateException

After executing this code:

try
{
    DoSomething();
}
catch (TaskCanceledException e)
{
    DealWithCancelledTaskException(e);
    throw;
}
catch (Exception e)
{
    DealWithNormalException(e);
    throw;
}

The exception is raised. DoSomething is supposed to throw TaskCancelledException , but it throws System.AggregateException containing one exception of type TaskCancelledException and is caught as normal Exception .

How can I catch this exception as TaskCancelledException ?

It is most likely that your code is throwing an AggregateException

Firstly try explicitly catching AggregateException. Then to access the exception that has been wrapped up by the aggregate exception use the InnerException Property. You can also access the list of all exceptions that have been aggregated (if there is or could be more than 1) by accessing the InnerExceptions property which gives you a list of the exceptions that this exception has aggregated

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