Catching task exceptions that spawns an internal task

Given the following simplified code

/// <summary> /// Within the VS2010 debugger, the following test will cease with an /// "Exception was unhandled by user code". /// (Debug window reports a "A first chance exception of type /// 'System.Exception' ..." BEFORE the exception is caught /// further down in the execution path.) /// OUTSIDE the VS2010 debugger, the exception is caught by the tComplete /// task that follows the tOpen task, just as expected. /// </summary> public void StartToOpen_Simple() { Task tOpen = Task.Factory.StartNew(() => { //do some work before spawning another task here try { return Task.Factory.StartNew(() => { Thread.Sleep(2000); //First chance exception occurs here: throw new Exception("Some generic exception"); }, TaskCreationOptions.AttachedToParent); } catch (Exception ex) { //never fires var source = new TaskCompletionSource<object>(); source.TrySetException(ex); return source.Task; } }).Unwrap(); Task tComplete = tOpen.ContinueWith(t => { if (t.Exception != null) { Exception LastOpenException = t.Exception.Flatten().GetBaseException(); if (LastOpenException is OperationCanceledException) { Console.WriteLine("OperationCanceledEx: " + LastOpenException.Message); } else { Console.WriteLine("Some exception occured in the tOpen task, but we're prepared for it here in the tComplete task."); Console.WriteLine("The exception message was: {0}", LastOpenException.Message); } } else { //do something if no exception occured (doesn't happen in this example) } }, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.AttachedToParent, TaskScheduler.Default); } 

and testing it, for example, through

  static void Main(string[] args) { AsyncTest test = new AsyncTest(); test.StartToOpen_Simple(); Console.WriteLine("Started async task. Waiting for exception"); Console.ReadKey(); } 

I observe a very annoying problem during its launch in the VS2010 debugger: as in the Summary case, the debugger breaks when throwing in the "tOpen" task, believing that I did not catch the exception (which I make "further below" in the "tComplete" task "). Only if I continue the debugging session can I see that the exception is "bubbling" and therefore handled as desired. If this method runs at a regular interval of time (which it is!) Debugs, this becomes a nightmare because the debugger is divided into each interval.

Running the program on the console does not exhibit this behavior.

  • Can someone explain to me why the Debugger breaks in this line, that is, it does not see
  • What are my options to intelligently debug code inside VS2010 where such code exists?
+4
source share
1 answer

The first random exception messages most often do not mean that there is a problem in the code. For applications / components that gracefully handle exceptions, exception random exception messages let the developer know that an exception has been detected and has been processed.

Please contact this

0
source

Source: https://habr.com/ru/post/1497234/


All Articles