What is the correct 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 the try block. Accepting wait commands, the compiler displays the following warning:
Since this call is not expected, execution of the current method continues until the call is completed. Consider applying the wait statement to a call result
I do not like these warnings in my code because in several places people forgot to use await and therefore it is difficult to find errors.
Change the message dialog operator to var task = new MessageDialog(ex.Message).ShowAsync().AsTask(); eliminates all warnings and errors, but I'm not sure if this is a good way to do this (and technically this is bad for the same reason that he wants me to await call)
Finally, I tried to save the exception and make my logic of what to show the user (including the entire logic of determining what type of exception was thrown) outside of catch, by:
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'm not sure I feel that this is the best way to do this. Any ideas how to do this?
source share