Try-Catch子句中的自定义例外和消息对话框

时间:2015-09-16 18:55:10

标签: c# async-await try-catch custom-exceptions messagedialog

我开发了一个通用应用程序,它需要几个自定义异常来捕获调用webservices时遇到的错误:NoInternetAccessException,NoJSONException,UserTimeoutException,...

以下是其中一个类

的示例
    public class NoInternetAccessException : Exception
    {
        private DateTime time;

        public DateTime Time { get { return time; } }

        public NoInternetAccessException(string message, DateTime time) 
            : base(message)
        {
            this.time = time;
        }
    }

我在几个地方发现了这些例外情况:

JSONParser 中,我在其中创建URI并调用Client.GetAsync方法:

    ...
    try
    {
        CancellationTokenSource cts = new CancellationTokenSource(Timeout);
        response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token);
        if (response.IsSuccessStatusCode)
            return response;                   
    }
    catch (TaskCanceledException)
    {
        if ((_currentRetries == MaxRetries)
            throw new UserTimeoutException("User Timeout Exception", DateTime.Now);
        else
            _currentRetries++;
    }
    catch (Exception e)
    {
        if (e.HResult == -2147012889)
            throw new WrongUrlException("Wrong URL Exception", e, DateTime.Now);
    }
    ...

WebService助手中,我管理所有的webservices调用:

    public static async Task<Infos> GetInfos(String url, List<KeyValuePair<String, String>> parameters, String protocol)
    {        
        var response = await JSONParser.getJSONFromUrl(url, parameters, "");
        Infos infos = new Infos();

        try
        {
            WsResponse wsResponse = JsonConvert.DeserializeObject<Infos>(response.ToString());
            infosCe = JsonConvert.DeserializeObject<Infos>(wsResponse.data.ToString());
            return infosCe;
        }
        catch (Exception e)
        {
            throw new DeserializeException("Deserialize exception", e, DateTime.Now, "Infos");
        }
    }

在调用webservices之后,所有这些例外都是最终由ViewModels 捕获:

    private async Task<Infos> WebServiceGetInfos()
    {
        ...
        try
        {
            Infos infos = await WebServices.GetInfos(url, parameters, "");
            return infosCe;
        }

        // Exceptions
        catch (DeserializeException dE)
        {
            ExceptionsMsgboxHelper.MsgboxDeserialize(dE);
            return null;
        }
        catch (NoInternetAccessException niaE)
        {
            ExceptionsMsgboxHelper.MsgboxNoInternetAccess(niaE, true, "");
            return null;
        }
        catch (NoJSONException njsonE)
        {
            ExceptionsMsgboxHelper.MsgboxNoJSON(njsonE);
            return null;
        }
        ...
    }

我希望每个异常调用 ExceptionsMsgboxHelper 帮助程序,它会显示每个异常的特定消息对话框:

    public async static void MsgboxNoJSON(NoJSONException njsonE)
    {
        Windows.UI.Popups.MessageDialog msgbox =
        new Windows.UI.Popups.MessageDialog("There is a problem when retrieving data. If the problem persists, please contact your administrator.",
            "Unexpected data received");
        await msgbox.ShowAsync();
    }

=&GT;但这不起作用,因为Message Dialog在Try-Catch子句中不起作用......

我也在stackoverflow上寻找解决方案:

Message dialog not showing in catch clause

Try-Catch doesn't show Message Dialog box with await

但我不知道如何使这个解决方案适应我的情况,因为我使用了几个自定义例外:

    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();
    }

=&GT;这样做是否比将此代码复制到每个自定义异常更好?

    ...
    DeserializeException dEx = null;
    NoInternetAccessException niaEx = null;
    NoJSONException njsonEx = null;
    try
    {
        Infos infos = await WebServices.GetInfos(url, parameters, "");
        return infosCe;
    }

    // Exceptions
    catch (DeserializeException dE)
    {
        dEx = de;
    }
    catch (NoInternetAccessException niaE)
    {
        niaEx = niaE;
    }
    catch (NoJSONException njsonE)
    {
        njsonEx=njsonE;
    }
    ...
    if (dEx != null)
    {
        ExceptionsMsgboxHelper.MsgboxDeserialize(dEx);
        return null;
    }
    if (niaEx != null)
    {
        ExceptionsMsgboxHelper.MsgboxNoInternetAccess(niaEx);
        return null;
    }
    if (njsonEx != null)
    {
        ExceptionsMsgboxHelper.MsgboxNoJSON(njsonEx);
        return null;
    }
    ...

=&GT;它似乎并不强大或可维护......

1 个答案:

答案 0 :(得分:0)

我终于通过捕获一个通用异常并测试它来解决我的问题:

    ...
    Exception ex = null;
    try
    {
        Infos infos = await WebServices.GetInfos(url, parameters, "");
        return infosCe;
    }

    // Exceptions
    catch (Exception e)
    {
        ex = e;
    }
    if (ex != null)
    {
        if (ex is DeserializeException)
        {
            DeserializeException dE = (DeserializeException)ex;
            ExceptionsMsgboxHelper.MsgboxDeserialize(dE);
        }
        if (ex is NoInternetAccessException)
        {
            NoInternetAccessException niaE = (NoInternetAccessException)ex;
            ExceptionsMsgboxHelper.MsgboxNoInternetAccess(niaE);
        }
        if (ex is NoJSONException)
        {
            NoJSONException njsonE = (NoJSONException)ex;
            ExceptionsMsgboxHelper.MsgboxNoJSON(njsonE);
        }
        ...
    }
    return null;

<强> =&GT;如果您认为有更好的方法,请指出我。

相关问题