I\'m trying to understand why the following code:
async void Handle_Clicked(object sender, System.EventArgs e)
{
try
{
await CrashAsync(\"aaa
You await Task
returned by ContinueWith
, so you observe exception related to this Task
- that it was cancelled (TaskCanceledException
). But you don't observe original exception thrown by CrashAsync (so "CrashAsync aaa") exception, hence the behavior you observe.
Here is sample code to get more understanding:
static async void Test() {
var originalTask = CrashAsync("aaa");
var onSuccess = originalTask.ContinueWith(async (t) =>
{
await CrashAsync("bbb");
}, TaskContinuationOptions.OnlyOnRanToCompletion);
var onFault = originalTask.ContinueWith(t => {
Log("Observed original exception: " + t.Exception.InnerExceptions[0].Message);
}, TaskContinuationOptions.OnlyOnFaulted);
}
So in short - just await your task and catch exception if any. You don't need to use ContinueWith
at all, because if you use await - the rest of the method is already a continuation.