Skip to main content
4
Theodor Zoulias
  • 46.1k
  • 8
  • 112
  • 155
Post Closed as "Duplicate" by Theodor Zoulias c#
added 3 characters in body
Source Link
Grant H.
  • 3.7k
  • 2
  • 40
  • 55

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, async listEntry => { await Task.Delay(15000); throw new Exception("Exception"); }); } catch (Exception ex) { //never hits, the application crashes } Console.ReadLine(); 

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, listEntry => { throw new Exception("Exception"); }); } catch (Exception ex) { //exception is caught, application continues } Console.ReadLine(); 

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, async listEntry => { await Task.Delay(1); throw new Exception("Exception"); }); } catch (Exception ex) { //never hits, the application crashes } Console.ReadLine(); 

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, listEntry => { throw new Exception("Exception"); }); } catch (Exception ex) { //exception is caught, application continues } Console.ReadLine(); 

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, async listEntry => { await Task.Delay(5000); throw new Exception("Exception"); }); } catch (Exception ex) { //never hits, the application crashes } Console.ReadLine(); 

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, listEntry => { throw new Exception("Exception"); }); } catch (Exception ex) { //exception is caught, application continues } Console.ReadLine(); 
deleted 74 characters in body
Source Link
Grant H.
  • 3.7k
  • 2
  • 40
  • 55

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, async listEntry => { await Task.Run(Delay() => { }1); throw new Exception("Exception"); }); } catch (Exception ex) { //never hits, the application crashes } Console.ReadLine(); 

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, listEntry => { throw new Exception("Exception"); }); } catch (Exception ex) { //exception is caught, application continues } Console.ReadLine(); 

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, async listEntry => { await Task.Run(() => { }); throw new Exception("Exception"); }); } catch (Exception ex) { //never hits, the application crashes } Console.ReadLine(); 

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, listEntry => { throw new Exception("Exception"); }); } catch (Exception ex) { //exception is caught, application continues } Console.ReadLine(); 

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, async listEntry => { await Task.Delay(1); throw new Exception("Exception"); }); } catch (Exception ex) { //never hits, the application crashes } Console.ReadLine(); 

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, listEntry => { throw new Exception("Exception"); }); } catch (Exception ex) { //exception is caught, application continues } Console.ReadLine(); 
Source Link
Grant H.
  • 3.7k
  • 2
  • 40
  • 55

Why does an exception in an async Parallel.ForEach crash the application?

Why does the following crash if run inside a console application instead of throwing an AggregateException and being caught by the outer try/catch?

I've simplified the use case for the await for brevity, but in the relevant code I am indeed trying to execute an awaitable Task of importance.

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, async listEntry => { await Task.Run(() => { }); throw new Exception("Exception"); }); } catch (Exception ex) { //never hits, the application crashes } Console.ReadLine(); 

I note that the following does not cause the application to fail, and an exception is indeed caught, but I do not understand what's really going on as to what is fundamentally different about the two contexts:

var list = new List<string>() {"Error"}; try { Parallel.ForEach(list, new ParallelOptions() { MaxDegreeOfParallelism = 8 }, listEntry => { throw new Exception("Exception"); }); } catch (Exception ex) { //exception is caught, application continues } Console.ReadLine();