“.Net Maui Android异常未传递到我的try-catch块中”

huangapple go评论79阅读模式
英文:

.Net Maui Android Exceptions, not bubbling up to my try, catch

问题

以下是代码部分的中文翻译:

我在捕获异常时遇到问题,因为Android不会将异常传递到我的异常处理块。这是一个简单的示例:

try
{
    var x = new Tuple<double, double>(Convert.ToDouble(xmlDeser.Cell.Latitude), Convert.ToDouble(xmlDeser.Cell.Longitude));
    return x;
}
catch (Exception ex)
{
    DisplayAlert("帮助", ex.Message, "确定");
}

在这里,代码块中的 trycatch 部分被翻译成了中文。如有其他需要,请告诉我。

英文:

I am having problems catching my exceptions due to Android not bubbling up my exceptions. A simple example:

try
{
    var x = new Tuple&lt;double, double&gt;(Convert.ToDouble(xmlDeser.Cell.Latitude), Convert.ToDouble(xmlDeser.Cell.Longitude));
    return x;
}
catch (Exception ex)
{
    DisplayAlert(&quot;Help&quot;,ex.Message, &quot;ok&quot;);
}

instead of going into my catch block, I get an error that puts my code in break mode when x is set:

0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception	C#
0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5	C#
0x1D in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23,26	C#
0x17 in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw	C#
0x6 in System.Threading.Tasks.Task.&lt;&gt;c.&lt;ThrowAsync&gt;b__128_0	C#
0xC in Android.App.SyncContext. at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:36,19	C#
0xE in Java.Lang.Thread.RunnableImplementor.Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36,6	C#
0x8 in Java.Lang.IRunnableInvoker.n_Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net7.0/android-33/mcw/Java.Lang.IRunnable.cs:84,4	C#
0x8 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22,5	C#

Which defeats the purpose of a try, catch block. As mentioned, I'm in break mode after, so never get into my catch block (even after trying to continue).

I added the tag to xamarin, because I'm curious if this exists in xamarin too, and It's not maui specific.

答案1

得分: 1

I have put your code in the Maui Project Template's button clicked event, and it will run successfully. Such as:

private void OnCounterClicked(object sender, EventArgs e)
{
    count++;

    if (count == 1)
        CounterBtn.Text = $"Clicked {count} time";
    else
        CounterBtn.Text = $"Clicked {count} times";

    SemanticScreenReader.Announce(CounterBtn.Text);

    try
    {
        var x = new Tuple<double, double>(Convert.ToDouble("string1"), Convert.ToDouble("string2"));
    }
    catch (Exception ex)
    {
        DisplayAlert("help", ex.Message, "ok");
    }
}

And the result message:

“.Net Maui Android异常未传递到我的try-catch块中”

So the cause should be you didn't run the code DisplayAlert("help", ex.Message, "ok"); on the main thread. For Android, all the code about the UI updating must execute on the main thread.

You can change your code to:

catch (Exception ex)
{
    MainThread.BeginInvokeOnMainThread(() => {
        DisplayAlert("help", ex.Message, "ok");
    });
}
英文:

I have put your code in the Maui Project Template's button clicked event, and it will run successfully. Such as:

      private void OnCounterClicked(object sender, EventArgs e)
      {
            count++;

            if (count == 1)
                  CounterBtn.Text = $&quot;Clicked {count} time&quot;;
            else
                  CounterBtn.Text = $&quot;Clicked {count} times&quot;;

            SemanticScreenReader.Announce(CounterBtn.Text);
            try
            {
                  var x = new Tuple&lt;double, double&gt;(Convert.ToDouble(&quot;string1&quot;), Convert.ToDouble(&quot;string2&quot;));
               // var x = new Tuple&lt;double, double&gt;(Convert.ToDouble(new string[] {&quot;aa&quot;,&quot;bb&quot;}), Convert.ToDouble(new string[] { &quot;aa&quot;, &quot;bb&quot; }));
            }
           catch (Exception ex)
           {
              DisplayAlert(&quot;help&quot;, ex.Message, &quot;ok&quot;);   
           }
    }

And the result message:

“.Net Maui Android异常未传递到我的try-catch块中”

So the cause should be you didn't run the code DisplayAlert(&quot;help&quot;, ex.Message, &quot;ok&quot;); on the mainthread. For the android, all the code about the UI updating must excute on the main thread.

You can change you code to:

catch (Exception ex)
{
   MainThread.BeginInvokeOnMainThread(() =&gt; {
      DisplayAlert(&quot;help&quot;, ex.Message, &quot;ok&quot;);
   });           
} 

huangapple
  • 本文由 发表于 2023年3月31日 23:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定