I have a Xamarin Forms app in which I am trying to set up a global exception handler. (.NET Standard 2.1, Microsoft Visual Studio Community 2022 Version 17.2.4 ) I used code from microsoft here.
I am using only the Android platform version of the app.
In AppShell.xaml.cs I have this:
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(wmsGlobalExceptionHandler);
// Same result with the next line instead of the line above
// currentDomain.UnhandledException += wmsGlobalExceptionHandler;
public void wmsGlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("pds: wmsGlobalExceptionHandler was hit.");
}
Not sure if AppShell.xaml.cs is the right place for ^ that.
In the MainPage.xaml I have a button (TestButton1) that hits the following method in the MainPageViewModel.cs
public async void TestButton1Clicked()
{
throw new System.Exception("Test Button 1 Clicked");
}
I tap TestButton1, and when the code hits the above line, I press F5 and the System.Exception throws and then a System.NullReferenceException throws and then I get "The application is in break mode" There is nothing in the Output window that indicates the problem, and the Call Stack just says External Code.
When I do the same steps running the app directly on the device, without Visual Studio involved, the app just crashes.
What am I doing wrong to get a global exception handler working in the Xamarin Forms app? Thanks.