How to handle Task.Factory.StartNew exception?

I have a strange problem. The exception thrown by Task is not always handled independently, how can I handle it.

I try this:

http://msdn.microsoft.com/en-us/library/dd997415%28v=vs.110%29.aspx

private class MyCustomException : Exception { public MyCustomException(String message) : base(message) { } } public static void Main() { var task1 = Task.Factory.StartNew(() => { throw new MyCustomException("I'm bad, but not too bad!"); }); try { task1.Wait(); } catch (AggregateException ae) { // Assume we know what going on with this particular exception. // Rethrow anything else. AggregateException.Handle provides // another way to express this. See later example. foreach (var e in ae.InnerExceptions) { if (e is MyCustomException) { Console.WriteLine(e.Message); } else { throw; } } } Console.Read(); } 

http://dotnetcodr.com/2014/02/11/exception-handling-in-the-net-task-parallel-library-with-c-the-basics/

http://blogs.msdn.com/b/pfxteam/archive/2010/08/06/10046819.aspx

 var task = Task.Factory.StartNew(() => this.InitializeViewModel(myViewModel)); task.ContinueWith(o => MyErrorHandler(task.Exception), TaskContinuationOptions.OnlyOnFaulted); 

and check out many other similar questions in StackOverflow. But this is always the same: the exception is not handled. It is not processed in these primitive code snippets! I think there is some magic here ... I am working on .Net Framework 4.0

Meanwhile, the only way to handle the exception that works for me is:

  Task.Factory.StartNew(() => { try { //do something that thrown exception } catch (Exception) { } }); 
+6
c # exception exception-handling task-parallel-library
source share
2 answers

If you run this sample code in Visual Studio, you really get the message MyCustomException was unhandled by user code , and Visual Studio will break into this line.

This does not mean that your exception is indeed unhandled. It just means that by default Visual Studio throws exceptions that are not handled inside the Task. You can verify this by running the application without debugging (Ctrl-F5); You will notice that your exception is handled as expected.

This issue is described in more detail in the following SO question:

  • Stop visual studio from hacking task exceptions
+7
source share

Microsoft Visual Studio debugging tools for managed code have the option "Just My Code". This option allows you to debug the debugger immediately when the exception leaves the user code (the task delegate in your example) and enters the non-user code (internal TPL in your example). This feature was developed before the introduction of TPL, to simplify the debugging of .NET applications, looking only at the code that you wrote, and ignore other code, for example, system calls. Just My Code hides non-user code so that it does not appear in the debugger windows. When you step, the debugger goes through any non-user code, but does not stop there.

But now the function "Only my code" interferes with the implementation of TPL.

There are four ways to solve this problem:

+3
source share

All Articles