WP8 - Parse 的 try-catch await 方法

WP8 - try-catch await method for Parse

我正在(成功地)使用 Parse SDK 进行推送通知,但是我想处理订阅 Parse 服务器失败的情况(通常是由于互联网连接不良)。

然而,似乎异常被抛入 Parse SDK 中,没有被处理,它最终以默认方法 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) 结束,这不是我想要的。

这是代码

        try
        {
            this.Startup += async (sender, args) =>
            {
                try
                {
                    // This optional line tracks statistics around app opens, including push effectiveness:
                    ParseAnalytics.TrackAppOpens(RootFrame);

                    // By convention, the empty string is considered a "Broadcast" channel
                    // Note that we had to add "async" to the definition to use the await keyword
                    await ParsePush.SubscribeAsync("");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("jupiii");
                }
            };
        }
        catch (Exception ex)
        {
            Debug.WriteLine("jupiii");
        }

我知道外面的try-catch是没有意义的,但我还是试了一下确定,那里没有处理。

没有互联网连接,应用程序崩溃并出现异常

[Parse.ParseException] = {Parse.ParseException: Invalid response from server ---> System.ArgumentException: Input JSON was invalid.
   at Parse.Internal.Json.Parse(String input)
   at Parse.ParseClient.DeserializeJsonString(String jsonData)
   at Parse.ParseClient.<>c__DisplayCl...

我认为问题可能出在异步方法中,但我正在等待它,如果我想捕获异常,这应该是正确的做法。


完整堆栈跟踪

Parse.ParseException: Invalid response from server ---> System.ArgumentException: Input JSON was invalid.
   at Parse.Internal.Json.Parse(String input)
   at Parse.ParseClient.DeserializeJsonString(String jsonData)
   at Parse.ParseClient.<>c__DisplayClassb.<RequestAsync>b__a(Task`1 t)
   --- End of inner exception stack trace ---
   at Parse.ParseClient.<>c__DisplayClassb.<RequestAsync>b__a(Task`1 t)
   at Parse.Internal.InternalExtensions.<>c__DisplayClass1`2.<OnSuccess>b__0(Task t)
   at Parse.Internal.InternalExtensions.<>c__DisplayClass7`1.<OnSuccess>b__6(Task t)
   at System.Threading.Tasks.ContinuationResultTaskFromTask`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Parse.ParseAnalytics.<>c__DisplayClass3.<<TrackAppOpens>b__2>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__3(Object state)

我现在做了很糟糕的事情只是为了让它工作,如果你有更好的解决方案,我很期待

    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        if (e.ExceptionObject.Message.Contains("Invalid response from server"))
        {
            e.Handled = true;
            return;
        }
        if (Debugger.IsAttached)
        {
            // An unhandled exception has occurred; break into the debugger
            Debugger.Break();
        }
    }