【问题标题】:WP8 - try-catch await method for ParseWP8 - Parse 的 try-catch 等待方法
【发布时间】:2015-06-04 16:14:08
【问题描述】:

我正在(成功地)使用 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)

【问题讨论】:

  • 您无法捕获由另一个线程中的一个线程抛出的异常,只能在抛出它的线程中捕获。因此,您必须在异步方法中捕获异步方法中发生的任何错误,否则整个程序会崩溃。
  • 但是如果我在等待它,它应该是相同的线程......?至少这就是我在 stackoverflow 上对这个问题的高级答案所发现的。
  • 你能得到完整的调用栈吗?这个被截断了
  • 对WP8不太熟悉,但是this.Startup,在你的上下文中this是什么?可能想检查是否存在可能的竞争条件?
  • 看起来是 WP8 的常见问题 :( tagwith.com/…

标签: c# visual-studio windows-phone-8 async-await


【解决方案1】:

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

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

【讨论】:

  • 问题出在ParsePush.SubscribeAsync内部。
  • 更新到最新的 sdk v.1.5.1 为我解决了这个问题。
猜你喜欢
  • 2020-01-01
  • 1970-01-01
  • 2021-11-09
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 2021-11-27
  • 1970-01-01
相关资源
最近更新 更多