I studied this article on MSDN, as well as some questions/answers on SO regarding this topic, but cannot figure why below code does not work (in a sample console app).

AggregateException is expected to be thrown, according to MSDN, which would contain one inner exception with hello message. Instead, this hello exception is unhandled. It happens when inside a debugger.

If you press continue or run standalone, it works as expected. Is there any way to avoid pressing continue all the time in VS? After all, whatever is within a Try...Catch block is considered handled in a single threaded programming model. Otherwise, debugging could be a nightmare.

VB.NET

Sub Main()
  Try
    Task.Factory.StartNew(AddressOf TaskThatThrowsException).Wait()
  Catch ex As AggregateException
    Console.WriteLine(ex.ToString) 'does not get here until you hit Continue
  End Try
End Sub

Private Sub TaskThatThrowsException()
  Throw New Exception("hello") 'exception was unhandled
End Sub

C#

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      try {
        Task.Factory.StartNew(TaskThatThrowsException).Wait();
      }
      catch (AggregateException ex) {
        Console.WriteLine(ex.ToString()); //never gets here                
      }
    }

    static void TaskThatThrowsException() {
      throw new Exception("hello"); //exception was unhandled            
    }
  }
}

Is there something obvious I am missing here?

Edit
Report