Monday, September 3, 2012

Windows 8: Cannot await in the body of a catch clause

While I was writing some async code in .NET 4.5, I decided to add some exception handling. So I created a try/catch block and wanted to show a message dialog when an exception occurs.

However when I added an await to wait for the message dialog result, the compiler started throwing exceptions:


The error message stated: “Cannot await in the body of a catch clause.”

I solved it by introducing a flag bool exceptionOccurred=false; and setting it to true in the catch block. After the catch clause I added some code to check if the flag is true.

catch(FileNotFoundException ex)

await new MessageDialog().ShowAsync();

Anyone with a cleaner solution?

1 comment:

Justin Liu said...

I noticed if you assign it to a variable n the warning goes away. :)

var result = dialog.ShowAsync();