How to fix “'System.AggregateException' occurred in mscorlib.dll”

后端 未结 4 1913
盖世英雄少女心
盖世英雄少女心 2020-12-14 14:12

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.

4条回答
  •  长情又很酷
    2020-12-14 14:44

    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
       }
    )
    

提交回复
热议问题