Exceptional Code

So it is not very often that I take the time to post something that isn’t, at least somewhat, informative, but I just ran into quite possibly the best error message ever and wanted to share it.

So from what I can tell from the following error, .NET found that my code was coded so well that it just had to bring it to my attention.

Exception Message: “One or more errors occurred.”

Stack Trace:

at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)

 

So there you have it.  .NET found my code to be so exceptional that it just had to let me know.  This might be dating back a bit, but I think this calls for a Jon Skeet fact:

Jon Skeet’s code is so well written that the only time he receives and error is when .NET runs System.Threading.Tasks.Task.ThrowIfExceptional().

 

So…  In case someone does stumble upon this post in search of information, I will attempt to explain how and why you might receive this error.  If you found this post in search of humor, laughs, and joy, then it is safe to stop reading, because the laughs and humor will cease from this point on.  However, the following information could potentially be joyous depending on the context.

 

 

Why and How?


Why might this exception occur?  In .NET there are tasks that are used to run asynchronous functions (tasks) through the use of multi-threading.  One such example is when you use the System.Threading.Timer class.  When the timer initiates your callback function, it is wrapped by libraries and functions that exist in the System.Threading.Tasks namespace.  Another example where you might see this type of exception is when using the Parallel functions that come along with .NET 4.0.  In most cases, since you’re using multi-threading, uncaught exceptions that are thrown are lost.  However, when using the Parallel libraries, the threads are joined together at the end, which allows the framework to pass along any exceptions to the calling point.

 

 

What to Do?


The exception in itself is not extremely helpful.  However, in most cases, there will be an InnerException that comes along with this exception.  Additionally, a full stack trace might lead you back to the part in your code that calls the Parallel function.

 

 

Example


Here is an example exception I received, along with an explanation:

Exception:

Exception Message: One or more errors occurred.

Stack Trace:

at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body)
at IT.CustomerRatingHelper.CustomerRatingProcess.FetchData(Object state)

Inner Exception:

Exception Message: Could not load file or assembly ‘IT.CustomerRatingHelper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=49ab5fd240ff1a65′ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0×80131040)

Stack Trace:

at IT.CustomerRatingHelper.CustomerRatingProcess.ProcessRow(CsvDataRow obj)
at System.Threading.Tasks.Parallel.<>c__DisplayClass32`2.<PartitionerForEachWorker>b__30()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass7.<ExecuteSelfReplicating>b__6(Object )

 

So, as you can see from the outer and inner exception above, a Parallel.ForEach statement is called from withing the CustomerRatingProcess.FetchData() function.  Furthermore, this Parallel.ForEach exists in order to make a call to the CustomerRatingProcess.ProcessRow function which is not loading properly due to some issues in referencing the assembly.

 

 

Conclusion


So hopefully, if nothing else, you have learned from this post that writing exceptional code is not always a good thing.

Add your comment Add your comment

Previously