MAUI: Global exception handling with error page or snack

1.6k Views Asked by At

I'm trying the new .Net MAUI to build an personnal project. At work we use Flutter and we used a global error handling to display an error page in case of API exceptions or widget rendering error (it catch everything) (like in React too) link here: https://docs.flutter.dev/testing/errors.

I'm trying to do the same thing with MAUI but I can't achieve this on iOS, the app still crash every time even if i use MarshalManagedException or any other thing I've found on web (I've used all solution posted on MAUI GitHub about Global Handling Exception). On Android and Windows, I can catch error and set the handled to true to prevent app from crashing.

What I want is to do the same thing as Flutter, catch exception and display an error page (evently with and stack hidden by default, as we do on Flutter app).

Can someone help me ? Or It can't be possible on MAUI (I want to try another tech than Flutter).

1

There are 1 best solutions below

1
On

"Global exception handler" in Maui is ONLY a chance to LOG details about what is crashing the app, before it goes away - is not a recoverable state. Because once the code gets there, Maui cannot guarantee it is safe to do anything displayable.

  • To avoid app crashing, have to wrap everything in try..catch. That means everywhere your code gets control. Every button handling method, etc.

Even then there are ways to crash Maui internals, or platform's graphics - but if you've try-catched everything, those are only extreme situations.

The theory is that only the app programmer can decide whether a situation can be safely recovered from, vs shutting down the app. try-catch gives you the place to decide that, separately for each code location.

I do wish Maui offered a simple way to wrap all user-interactions in a single exception-handler (that can recover and continue). But it doesn't.

(The Blazor part of Maui might, for web-based code. I'm not familiar with that.)