What is the proper way to show a message dialog due to a caught exception?
I originally tried
try
{
await DoSomething();
}
catch(InvalidOperation ex)
{
await MessageDialog(ex.Message).ShowAsync();
}
catch(CommunicationException)
{
await MessageDialog(StringResourceLoader.LoginError).ShowAsync();
}
This did not work because you cannot await
inside of a try block. Taking the await commands out makes the compiler shows the following warning:
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call
I don't like keeping those warnings in my code, because in several spots people have forgotten to use await
and thus had hard to find bugs.
Changing the message dialog statement to var task = new MessageDialog(ex.Message).ShowAsync().AsTask();
gets rid of all warnings and errors, but I am not sure that is a good way to go about it (and technically is bad for the same reason it wants me to await
the call)
Finally, I tried storing the exception and doing my logic of what to display to the user (including all logic on determining what type of exception was thrown) outside of the catch, via:
Exception thrownException = null;
try
{
await DoSomething();
}
catch(Exception ex)
{
thrownException = ex;
}
if (thrownException is InvalidOperationException)
await MessageDialog(ex.Message).ShowAsync();
else if (thrownException is CommunicationException)
await MessageDialog(StringResourceLoader.LoginError).ShowAsync();
I am not sure I feel that this is the best way to go about it either. Any ideas how this should be done?
Edit: After Hans' comment about other issues, I ended up solving it by creating the following class:
This has changed my try/catch statement to
Which makes things more stable in the long run (once I convert all message dialog callers to use this instead of
showAsyncing
themselves) and makes the try/catch block a lot less messy (imo).