Thread.Start
starts a new thread, but you are handling the exception in another thread:
try
{
new Thread(new ThreadStart(DoWork)).Start();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Task.Factory.StartNew
a new task starts. The task throws an exception inside it to set the property Status
:
var errorTask = Task.Factory.StartNew<Func<string>>(() =>
{
Console.WriteLine("in task");
// this exception will be caught in Task base code,
// since tasks catch exceptions thrown by task methods;
// note, that this will be wrapped into AggregateException
throw new Exception();
});
when you try to receive Task.Result
, and the task is in a failed state, it just throws an exception:
// this will re-throw exception in calling thread
string result = errorTask.Result;
That's why your second catch
catches him.
source
share