消息对话框没有catch子句展示 [英] Message dialog not showing in catch clause

查看:180
本文介绍了消息对话框没有catch子句展示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code在我的Windows 8应用功能捕获错误,并显示一个消息对话框

I am using the following code in my Windows 8 app function to catch an error and display an Message Dialog

 catch (Exception ex)
            {
                MessageDialog err = new MessageDialog("Error");
                await err.ShowAsync();


            }

不过,我得到一个错误catch子句的身体不能等待。

But I get an error "cannot await in the body of a catch clause".

但是,当我删除等待,它的工作原理,但我得到了code警告因为这个呼叫没有等待,目前的方法继续执行之前,通话完毕。考虑应用的await操作者调用的结果。

But when I remove the await, it works but I get a warning on the code "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".

我需要在这个catch子句显示一条消息,我该如何解决这个问题?

I need to display a message in this catch clause, how do I fix this?

推荐答案

而不是做在抓工作,只需设置例外的局部变量。如果它非空,那么你知道你需要的捕捞结束后处理它块。

Rather than doing the work in the catch, just set the exception to a local variable. If it's non-null, then you know you need to handle it after the end of the catch block.

public static async Task Foo()
{
    Exception e = null;
    try
    {
        //just something to throw an exception
        int a = 0;
        int n = 1 / a;
    }
    catch (Exception ex)
    {
        e = ex;
    }
    if (e != null)
        await ShowDialog();
}

这篇关于消息对话框没有catch子句展示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆