I found this code snippet (simplified version provided):
using System;
using System.Threading.Tasks;
namespace TaskTest
{
class Program
{
static void Main(string[] args)
{
var task = SendMessage();
task.Wait();
if (task.IsFaulted) // Never makes it to this line
{
Console.WriteLine("faulted!");
}
Console.Read();
}
private static async Task SendMessage()
{
await Task.Run(() => throw new Exception("something bad happened"));
}
}
}
I'm sure this is a bug since task.Wait();
throw
s and there is no catch
block.
Now I'm wondering when you would need to use task.IsFaulted
?