1

I do not understand why an exception thrown inside an async method is not caught when the await statement is surrounded by a try/catch, which becomes an unhandled exception crashing the app.

The reason for this exception is understood, I'm not expecting an answer for that. I was assuming the exception would be caught. I'm worried about the exception handling in the application not doing what is expected on future exceptions.

using Amazon.Lambda.Model;

InvokeResponse invokeResponse;
var lambdaRequest = new InvokeRequest {...};
try
{
    var task = _awsLambdaClient.InvokeAsync(lambdaRequest);
    invokeResponse = await task; <<== throws TaskCanceledException
}
catch (Exception e)
{
    Log.Error(...);
    return
}

C# environment is Mono, Xamarin.Android to be specific, though I imagine the same code compiled in a .NETCORE console app would repro as well, unless this is a bug in Mono which I doubt. I assume I'm misunderstanding something about exception handling.

UPDATE:

Tried the same code in a .NETCORE20 console application, and the TaskCanceledException is handled. So it's looking like a Mono/Xamarin.Android specific problem.

New Update: After being able to repro this for a few days, of course my repro stopped repro'ing and the exception is now caught in the Mono app too. Not really sure what happened.

mipnw
  • 2,135
  • 2
  • 20
  • 46
  • The task didn't cause an exception to be thrown, because the exception occurred inside of the task. This is the result of an "unobserved" exception. The result of the task's execution throwing an exception can be seen by by the task's completed status, which should be Faulted. – Travis J Apr 02 '18 at 21:04
  • Possible duplicate of [Unobserved Task exceptions in .NET 4.5 still crash app](https://stackoverflow.com/questions/16093846/unobserved-task-exceptions-in-net-4-5-still-crash-app) – Travis J Apr 02 '18 at 21:04
  • Also related: [Why doesn't this exception get thrown?](https://stackoverflow.com/q/23766808/1026459) – Travis J Apr 02 '18 at 21:05
  • https://msdn.microsoft.com/en-us/magazine/jj991977.aspx – pmcilreavy Apr 02 '18 at 21:05
  • @pmcilreavy, thank you, this link could be useful to future readers, in my case though I'm already familiar with these concepts. – mipnw Apr 02 '18 at 21:39

0 Answers0