57

I'm receiving an unhandled exception while debugging, and the program stops executing. The debugger doesn't show me the line so I don't know what to fix.

An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll

Additional information: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.

Cannot obtain value of local or argument '<this>' as it is not available at this instruction pointer, possibly because it has been optimized away. System.Threading.Tasks.TaskExceptionHolder

How can I troubleshoot this problem?

I also found this question which is pretty similar.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • I somehow caused this problem when a previously problem-free console application was interrupted during a debug run. Closing Visual Studio and then re-opening it resolved the problem and it then ran ok - this won't help in all cases but worth a try if the application 'should' work ok. – Gary Stacey Apr 11 '16 at 11:02

4 Answers4

65

As the message says, you have a task which threw an unhandled exception.

Turn on Break on All Exceptions (Debug, Exceptions) and rerun the program.
This will show you the original exception when it was thrown in the first place.


(comment appended): In VS2015 (or above). Select Debug > Options > Debugging > General and unselect the "Enable Just My Code" option.

T.Todua
  • 53,146
  • 19
  • 236
  • 237
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    Where is this setting? Debug > Exceptions > ...? – mdupls Jun 03 '14 at 13:07
  • 3
    @mdupls: Break on all CLR exceptions. – SLaks Jun 03 '14 at 13:09
  • 2
    just check them all. Then use the "Reset All" to come back to the default values. – Cătălin Rădoi Oct 17 '14 at 10:23
  • The Exception Breaker VS plugin will allow you to deal with this setting en masse. See http://stackoverflow.com/a/11122121/179972 I tried it in VS 2012 and it works. Easier than manually checking all the exception category boxes. – John K Apr 13 '15 at 00:02
  • 16
    In VS2015 (Enterprise). Select Debug / Options, then go to "Debugging / General" and unselect the "Enable Just My Code" option. – Lars Dec 18 '15 at 18:22
  • 1
    @Lars your answer (comment) is more correct and up-to-date. so, i've appended – T.Todua Mar 04 '18 at 11:23
12

You could handle the exception directly so it would not crash your program (catching the AggregateException). You could also look at the Inner Exception, this will give you a more detailed explanation of what went wrong:

try {
    // your code 
} catch (AggregateException e) {

}
Darren
  • 68,902
  • 24
  • 138
  • 144
4

The accepted answer will work if you can easily reproduce the issue. However, as a matter of best practice, you should be catching any exceptions (and logging) that are executed within a task. Otherwise, your application will crash if anything unexpected occurs within the task.

Task.Factory.StartNew(x=>
   throw new Exception("I didn't account for this");
)

However, if we do this, at least the application does not crash.

Task.Factory.StartNew(x=>
   try {
      throw new Exception("I didn't account for this");
   }
   catch(Exception ex) {
      //Log ex
   }
)
helios456
  • 1,624
  • 16
  • 24
1

In my case I ran on this problem while using Edge.js — all the problem was a JavaScript syntax error inside a C# Edge.js function definition.

Nono
  • 188
  • 1
  • 7