Replies: 18 comments 40 replies
-
I have the same question and I cannot find any solution. |
Beta Was this translation helpful? Give feedback.
-
What sort of thing are you looking for? How would you like it to behave when exceptions occur? |
Beta Was this translation helpful? Give feedback.
-
Storing errors to a remote logging service would be a common use-case already commonly used on Xamarin and similar products. |
Beta Was this translation helpful? Give feedback.
-
Cross linking dotnet/aspnetcore#40765 which handles the case of WPF/WinForms in Blazor Hybrid. If there's a canonical way to do global exception handling (like we have in WinForms/WPF), we need to ensure .NET MAUI Blazor Hybrid is able to integrate with it directly. WPF/WinForms: AppDomain.CurrentDomain.UnhandledException += (sender, error) =>
{
MessageBox.Show(text: error.ExceptionObject.ToString(), caption: "Error");
}; cc/ @hartez |
Beta Was this translation helpful? Give feedback.
-
Took me a long time to find this out, but this (or your own variation) will catch all exceptions (including the ones thrown by Xamarin which normally go un-noticed ;-) ). I haven't tried this in MAUI yet,, but I presume it'll work there too.
|
Beta Was this translation helpful? Give feedback.
-
In putting together Sentry error monitoring for .NET MAUI, we've faced this head-on. There are several different issues to deal with, as each platform that MAUI supports has its own quirks. Here's a class you can use in your own code that provides a global event handler that works on all major platforms. (I didn't test Tizen.) Usage: MauiExceptions.UnhandledException += (sender, args) => { /* your code */ } (If you decide to use Sentry, this is built-in.) |
Beta Was this translation helpful? Give feedback.
-
Any news ? I would like to do like in Flutter (https://docs.flutter.dev/testing/errors) and catch all kind of exceptions (even customs I've made) and display them in Error page but the solutions I've found here doesn't help me to do that. |
Beta Was this translation helpful? Give feedback.
-
Has anyone got a solution to prevent the application from closing when an exception occurs? |
Beta Was this translation helpful? Give feedback.
-
Thanks @TanayParikh and @mattjohnsonpint, I didn't know about UnhandledException - that'll teach me to read through ALL the Intellisense options when I'm trying something new! :-) |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? None of the given solutions work incredibly well, or only work in some scenarios/platforms. This seems like a super important feature to have for such a high-profile platform, but there seems to be no straight-forward way of doing it. |
Beta Was this translation helpful? Give feedback.
-
In maui .net 8 was there any solution? :( |
Beta Was this translation helpful? Give feedback.
-
Also interested. |
Beta Was this translation helpful? Give feedback.
-
Any update on this? |
Beta Was this translation helpful? Give feedback.
-
I am trying to catch ObjectDisposedException which causes the whole app to crash and I think it causes the image disposing... I really cannot modify the base MAUI Image component. Any updates? |
Beta Was this translation helpful? Give feedback.
-
Hi just to say that this would be a dream in Maui land but sorry to be cynical but will never happen. |
Beta Was this translation helpful? Give feedback.
-
Any updates on this yet?? |
Beta Was this translation helpful? Give feedback.
-
For an app targetting iOS and Android this should be more than enough: public static class GlobalExceptionHandler
{
// We'll route all unhandled exceptions through this one event.
public static event UnhandledExceptionEventHandler UnhandledException;
static GlobalExceptionHandler()
{
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
UnhandledException?.Invoke(sender, args);
};
// Events fired by the TaskScheduler. That is calls like Task.Run(...)
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
UnhandledException?.Invoke(sender, new UnhandledExceptionEventArgs(args.Exception, false));
};
#if IOS
// For iOS and Mac Catalyst
// Exceptions will flow through AppDomain.CurrentDomain.UnhandledException,
// but we need to set UnwindNativeCode to get it to work correctly.
//
// See: https://github.com/xamarin/xamarin-macios/issues/15252
ObjCRuntime.Runtime.MarshalManagedException += (_, args) =>
{
args.ExceptionMode = ObjCRuntime.MarshalManagedExceptionMode.UnwindNativeCode;
};
#elif ANDROID
// For Android:
// All exceptions will flow through Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser,
// and NOT through AppDomain.CurrentDomain.UnhandledException
Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) =>
{
args.Handled = true;
UnhandledException?.Invoke(sender, new UnhandledExceptionEventArgs(args.Exception, true));
};
Java.Lang.Thread.DefaultUncaughtExceptionHandler = new CustomUncaughtExceptionHandler(e =>
UnhandledException?.Invoke(null, new UnhandledExceptionEventArgs(e, true)));
#endif
}
}
#if ANDROID
public class CustomUncaughtExceptionHandler(Action<Java.Lang.Throwable> callback)
: Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler
{
public void UncaughtException(Java.Lang.Thread t, Java.Lang.Throwable e)
{
callback(e);
}
}
#endif And a simple way to use this would be:
|
Beta Was this translation helpful? Give feedback.
-
You can persist unhandled exceptions in Preferences with: // Install a global exception handler to persist the crash information before the app crashes.
#if WINDOWS
global::Microsoft.Maui.MauiWinUIApplication.Current.UnhandledException += (sender, e) =>
{
if (e.Exception is Exception ex)
{
Preferences.Default.Set("LastCrashTime", DateTimeOffset.Now.ToUnixTimeMilliseconds());
Preferences.Default.Set("LastCrashMessage", ex.Message);
Preferences.Default.Set("LastCrashStackTrace", ex.StackTrace);
}
e.Handled = false;
};
#else
global::System.AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
if (e.ExceptionObject is Exception ex)
{
Preferences.Default.Set("LastCrashTime", DateTimeOffset.Now.ToUnixTimeMilliseconds());
Preferences.Default.Set("LastCrashMessage", ex.Message);
Preferences.Default.Set("LastCrashStackTrace", ex.StackTrace);
}
};
#endif Then, on the next run of the app, you can casually retrieve any exceptions from the last 24 hours, and choose what to do with it. if (Preferences.Default.Get("LastCrashTime", 0L) is long lastCrashTime
&& DateTimeOffset.Now.ToUnixTimeMilliseconds() < lastCrashTime + 24 * 60 * 60 * 1000
&& Preferences.Default.Get("LastCrashMessage", string.Empty) is string lastCrashMessage
&& Preferences.Default.Get("LastCrashStackTrace", string.Empty) is string lastCrashStackTrace)
{
Trace.WriteLine($"The app crashed on {DateTimeOffset.FromUnixTimeMilliseconds(lastCrashTime).LocalDateTime} with the following message:\n\n{lastCrashMessage}\n\n{lastCrashStackTrace}");
// Shell.Current.GotoAsync(nameof(YourAppRecoveredFromASeriousCrashPage));
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
With the new host model in .NET Maui, will we get a straightforward way of catching exceptions?
Beta Was this translation helpful? Give feedback.
All reactions