Reputation: 433
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.
Upvotes: 0
Views: 2127
Reputation: 14554
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");
});
}
Upvotes: 2