I have a xamarin app that uses plugins for media access to select or take a photo.
In my relay command I have this:
try
{
var mediaFile =
await
_mediaPicker.SelectPhotoAsync(new CameraMediaStorageOptions());
using (var memoryStream = new MemoryStream())
{
mediaFile.Source.CopyTo(memoryStream);
...do image stuff here
}
}
catch (TaskCanceledException taskCanceled)
{
Debug.WriteLine(taskCanceled.Message);
}
Is there a better way to handle task cancellation exceptions in general? The SelectPhotoAsync doesn't take a cancellation token. Is an empty catch ok here? It is perfectly fine for a user to cancel the operation.
Thanks.
Generally, you should catch OperationCanceledException
rather than TaskCanceledException
. Other than that, your approach looks fine; the proper way to handle the exception is to catch it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With