英文:
.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, "确定");
}
在这里,代码块中的 try
和 catch
部分被翻译成了中文。如有其他需要,请告诉我。
英文:
I am having problems catching my exceptions due to Android not bubbling up my exceptions. A simple example:
try
{
var x = new Tuple<double, double>(Convert.ToDouble(xmlDeser.Cell.Latitude), Convert.ToDouble(xmlDeser.Cell.Longitude));
return x;
}
catch (Exception ex)
{
DisplayAlert("Help",ex.Message, "ok");
}
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.<>c.<ThrowAsync>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:
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 = $"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"));
// var x = new Tuple<double, double>(Convert.ToDouble(new string[] {"aa","bb"}), Convert.ToDouble(new string[] { "aa", "bb" }));
}
catch (Exception ex)
{
DisplayAlert("help", ex.Message, "ok");
}
}
And the result message:
So the cause should be you didn't run the code DisplayAlert("help", ex.Message, "ok");
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(() => {
DisplayAlert("help", ex.Message, "ok");
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论