I have been reading around a bit, such as here (Catch an exception thrown by an async void method) and I thought the behaviour of async Task
methods was:
- that you could try/catch them as normal when using
await
orTask.Wait
e.g.try{ await X();}catch(Exception SqlException e){...}
- if you let it 'bubble up' you will instead get
AggregateException
But I am finding my application is just terminating without breaking on exceptions at all. My code looks like:
internal async Task RunAsync()
{
var tasks = monitors.Select((p) => p.Value.MonitorAsync()).ToList();
await Task.WhenAny(tasks);
Console.WriteLine("ONE");
}
public static async Task Main(string[] args)
{
var app = new App();
try
{
await app.RunAsync();
Console.WriteLine("TWO");
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
Setting breakpoints on "ONE" and "TWO" I can see that tasks
has at least one Task
with status Faulted, and t
has status RanToCompletion. So the fault state is lost and no exceptions.
Clearly I'm missing something obvious, what should I be doing differently?
BTW WhenAny
is used to detect unusual termination, these tasks should only ever exit due to failure. This is more of a test-bed to understand catching exceptions.