If you call an async-returning method and never observe its Task, then:
-
Anything that throws before the first await
– is thrown synchronously on the caller’s thread
– always surfaces immediately (and will crash or bubble up)
-
Anything that throws after the first await** (or inside a Task.Run): – faults the Task produced by your async method – **no one is awaiting it**, so you never see the exception – by default (in .NET 4.5+ and .NET Core) the unobserved exception is *swallowed* – you can hook TaskScheduler.UnobservedTaskException` if you want to log it
Example A: synchronous throw (before any await)
async Task Foo()
{
int y = 0;
int x = 10/y; // throws synchronously
await Task.Delay(1);
}
void Caller()
{
// Caller() will immediately throw DivideByZeroException here
// because that code ran BEFORE the first await.
Foo();
}
Example B: asynchronous throw (after an await or inside Task.Run)
async Task Bar()
{
await Task.Yield();
int y = 0;
int x = 10/y; // faults the Task, but nothing awaits it
}
void Caller()
{
Bar(); // returns a faulted Task that nobody observes
// no exception here, program keeps running
}
Because you said “it exits the method silently,” you must be in scenario B: your division happens only after the first await or inside Task.Run, and nobody ever awaits or checks the returned Task, so the exception is never re-thrown on any thread you’re watching.
If you want to see or handle that exception, you have to:
- Await the Task
try
{
await run_simulation_async();
}
catch (DivideByZeroException ex)
{
Console.Error.WriteLine("Oops: " + ex);
}
- Or attach a continuation:
run_simulation_async()
.ContinueWith(t => Console.Error.WriteLine(t.Exception),
TaskContinuationOptions.OnlyOnFaulted);
- Or wrap your dangerous code in a try/catch inside the async method itself.
That way nothing goes “invisible.”