1

I am using (succesfully) Parse SDK for push notification, however I want to handle situation, when the subscribing to Parse server is unsucessfull (usually by bad internet connetion).

However it looks like Exception is threw inside Parse SDK, not handled and it ends up in default method private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) which is not what I want.

This is the code

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

I know that outer try-catch is meaningless, but I just tried it anyway to be sure, it is not handled there.

Without internet connection, the app crashes with exception

[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...

I thought that the problem could be in the async method, but I am awaiting it, which should be right thing to do, if I want catch exception.


Full stack trace

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)
libik
  • 22,239
  • 9
  • 44
  • 87
  • You cannot catch an exception that was thrown by one thread in a different thread, only in the thread that threw it. You therefore have to catch any errors that occur in an async method inside the async method or the whole program crashes. – Tony Vitabile Jun 03 '15 at 13:46
  • 2
    But if I awaiting it, it should be the samre thread...? At least thats what I found at high-ranked answers for this question on stackoverflow. – libik Jun 03 '15 at 14:22
  • Can you get the full callstack? This one is truncated – Kevin Gosse Jun 03 '15 at 14:49
  • Not too familiar with WP8, but `this.Startup`, what is `this` in your context? Might want to check if there's a possible race condition? – Niels Filter Jun 03 '15 at 14:58
  • Looks like a common issue for WP8 :( http://tagwith.com/question_624752_error-implementing-parse-com-push-notifications-in-wp8 – Niels Filter Jun 03 '15 at 15:13
  • The exception says that it received invalid JSON. – Paulo Morgado Jun 03 '15 at 16:43
  • The whole point of the `async / await` mechanism is to push the task off onto another Thread. The reason you have to wait for it is because it's running on another thread and the thread executing the `await` statement doesn't know when the other one is done running. If it were running on your thread, you'd just call the method directly & get control back when it returned. – Tony Vitabile Jun 03 '15 at 17:30
  • @KooKiz - it is not important, it throws exception because it gets no response from server, therefore it has invalid JSON to work with. It is ok to not subscribe and throw exception, but I just need to handle that exception. – libik Jun 04 '15 at 07:36
  • @NielsFilter - It is the "core" class of any WP8 application : `public partial class App : Application` – libik Jun 04 '15 at 07:36
  • @TonyVitabile - ok, but why [here](http://stackoverflow.com/questions/5383310/catch-an-exception-thrown-by-an-async-method) the accepted answer says `the exception will bubble up to the calling code - but it will only do so if you await or Wait() the call to Foo`? – libik Jun 04 '15 at 07:40
  • @libik I was asking the callstack not to know why it throws an exception, but in which context it throws it. It may help understanding why it's not forwarded to your task, and how to handle it (if there is a way) – Kevin Gosse Jun 04 '15 at 07:58
  • @KooKiz - ah, ok, I added it to my answer – libik Jun 04 '15 at 08:14

1 Answers1

0

I did terrible thing right now just to make it work, if you have better solution, I am looking forward to it

    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();
        }
    }
libik
  • 22,239
  • 9
  • 44
  • 87