Exception stack trace and aggregation in Parallel.ForEach

Shachaf.Gortler

I have some code I'm going to parallelize. It currently looks something like this:

foreach (var item in collection)
{
    if (error)
    {
        throw new Exception();
    }
}

The exception and its stack trace are saved in a log file.

After parallelization, it would look like this:

Parallel.ForEach(collection, item =>
{
    if (error)
    {
        throw new Exception();
    }
});
  1. How will this exception stop the parallel execution of the other items? Is there a way to stop all other executions once an error is encountered in one the loop threads?
  2. How will this influence the exception stack trace?
Evk
  1. When an exception is thrown, execution of the items that have not yet been scheduled will be cancelled. Items that are already scheduled, however, will not and cannot be cancelled. (Remember, this happens in parallel, which is why you use Parallel in the first place.) They will run to the end, and may themselves throw exceptions.

  2. Because of this, there might be multiple exceptions thrown in a parallel loop. That is why they are always wrapped in an AggregateException, even when an exception was thown only once. You can catch that AggregateException and enumerate through its InnerExceptions property, which contains all exceptions thrown, together with their stack traces:


try
{
    Parallel.ForEach(collection, item =>
    {
        if (error)
        {
            throw new Exception();
        }
    });
}
catch (AggregateException ex)
{
    foreach (var exception in ex.InnerExceptions)
    {
        // do something       
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exception Stack Trace Missing Items

From Dev

Why is the exception stack trace not logged in?

From Dev

Parallel Foreach throw an ApartmentState exception

From Java

How to print the stack trace of an exception object in Python?

From Dev

Exception that emails stack trace when raised

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

Rethrow an Exception with correct line numbers in the stack trace

From Dev

How can an Exception be created/thrown with no stack trace?

From Dev

logback: newline before exception stack trace but not otherwise?

From Dev

Suppressing stack trace when catching exception

From Dev

How to print the stack trace of an exception object in Python?

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

cxf null pointer exception without stack trace

From Dev

Printing the stack trace vs the exception itself

From Dev

How does the console obtain the stack trace of an exception?

From Java

Get exception description and stack trace which caused an exception, all as a string

From Dev

Exception thrown from Parallel Stream.forEach

From Dev

Out of Memory Exception in Parallel.ForEach

From Dev

DataTable throws exception in Parallel.ForEach

From Dev

How to print the selenium exception stack trace in Extent reports

From Dev

Why does android logcat not show the stack trace for a runtime exception?

From Dev

How to get stack trace string without raising exception in python?

From Dev

Unit Test Adapter threw exception with no stack trace in TeamCity 7.1.5

From Dev

Throwing an exception more than once loses its original stack trace

From Dev

Log4j2: log stack trace without an exception

From Dev

Facelets custom error page - customize / wrap the exception message / stack trace

From Dev

java.lang.reflect.Proxy: Huge exception stack trace

Related Related

  1. 1

    Exception Stack Trace Missing Items

  2. 2

    Why is the exception stack trace not logged in?

  3. 3

    Parallel Foreach throw an ApartmentState exception

  4. 4

    How to print the stack trace of an exception object in Python?

  5. 5

    Exception that emails stack trace when raised

  6. 6

    locals() and globals() in stack trace on exception (Python)

  7. 7

    How to use a stack trace dump to debug an exception?

  8. 8

    Rethrow an Exception with correct line numbers in the stack trace

  9. 9

    How can an Exception be created/thrown with no stack trace?

  10. 10

    logback: newline before exception stack trace but not otherwise?

  11. 11

    Suppressing stack trace when catching exception

  12. 12

    How to print the stack trace of an exception object in Python?

  13. 13

    locals() and globals() in stack trace on exception (Python)

  14. 14

    How to use a stack trace dump to debug an exception?

  15. 15

    cxf null pointer exception without stack trace

  16. 16

    Printing the stack trace vs the exception itself

  17. 17

    How does the console obtain the stack trace of an exception?

  18. 18

    Get exception description and stack trace which caused an exception, all as a string

  19. 19

    Exception thrown from Parallel Stream.forEach

  20. 20

    Out of Memory Exception in Parallel.ForEach

  21. 21

    DataTable throws exception in Parallel.ForEach

  22. 22

    How to print the selenium exception stack trace in Extent reports

  23. 23

    Why does android logcat not show the stack trace for a runtime exception?

  24. 24

    How to get stack trace string without raising exception in python?

  25. 25

    Unit Test Adapter threw exception with no stack trace in TeamCity 7.1.5

  26. 26

    Throwing an exception more than once loses its original stack trace

  27. 27

    Log4j2: log stack trace without an exception

  28. 28

    Facelets custom error page - customize / wrap the exception message / stack trace

  29. 29

    java.lang.reflect.Proxy: Huge exception stack trace

HotTag

Archive