Xamarin Studio社区将处理的异常视为未处理的异常

时间:2017-06-13 08:23:06

标签: xamarin parse-platform xamarin.ios xamarin.android xamarin-studio

我遇到这个问题,我的Xamarin Android应用程序出现未处理的异常崩溃。我正在尝试使用用户会话令牌将用户登录到parse.com。这些语句放在try-catch块中以捕获异常。但是catch块无法从parse.com和应用程序崩溃中捕获“无效会话令牌”错误。如果我手动篡改sessionToken,Catch块工作正常。

这就是我正在做的事情

try {
   await ParseUser.BecomeAsync(sessionToken);
   ...
} catch (Exception e){
//log exception
}

当会话令牌无效错误时,上面的代码崩溃说“未处理的异常....”即使它被放在try块中。

以下代码有效。 (手动篡改令牌)

try {
   await ParseUser.BecomeAsync(sessionToken + "test");
   ...
} catch (Exception e){
//log exception
}

Xamarin Component store

解析组件

我错过了什么?

此外,相同的代码适用于Xamarin.iOS。

编辑添加StackTrace

  at Parse.Internal.ParseCommandRunner+<>c__DisplayClass2.<RunCommandAsync>b__1 (System.Threading.Tasks.Task`1[TResult] t) [0x000eb] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
  at Parse.Internal.InternalExtensions+<>c__DisplayClass1`2[TIn,TResult].<OnSuccess>b__0 (System.Threading.Tasks.Task t) [0x00000] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
  at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x0006f] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
  at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00024] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <3fd174ff54b146228c505f23cf75ce71>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
  at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00024] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <3fd174ff54b146228c505f23cf75ce71>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at Parse.Internal.InternalExtensions+<>c__DisplayClass7`1[TResult].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <5d1c0c2b96a7483d85e5c63b3e156125>:0 
  at System.Threading.Tasks.ContinuationResultTaskFromTask`1[TResult].InnerInvoke () [0x00024] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <3fd174ff54b146228c505f23cf75ce71>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0 
  at Hyphen_App.Droid.SplashActivity+<setCurrentUser>d__4.MoveNext () [0x00035] in /Chethan Shetty/GIT/android-parent/Hyphen_App.Droid/Splash/SplashActivity.cs:112

编辑1 将setCurrentUser更改为以下后,我能够捕获异常,但3-4秒后应用程序自身崩溃,发出“System.AggregateException”

 void setCurrentUser(string sessionToken,Action<bool> callback){
       try {
             ParseUser.BecomeAsync(sessionToken).Wait();
             callback(true);
       }catch(AggregateException e){
             e.Handle( x => { return true; });
             callback(false);
       }

1 个答案:

答案 0 :(得分:0)

正如您在评论中所说,您在async中调用了OnCreate方法而没有await

如果异步堆栈中的某个位置没有等待任务,则可能导致异常无法被捕获。

我希望它能帮助您解决问题。

更新

很抱歉没有清楚地表达我的意思。您的代码必须类似:

    protected override async void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        await this.SetCurrentUser();
    }

    private async Task<bool> SetCurrentUser(string sessionToken)
    {
        try {
            await ParseUser.BecomeAsync(sessionToken);
        } catch (Exception e) {
            return false;
        }
    }

使用await关键字是我在说'等待'时所指的。

使用C#中的async / await关键字,回调很少在异步场景中使用。在microsoft docs

上有一个简短的介绍