MAUI: 使用错误页面或小吃来进行全局异常处理

huangapple go评论101阅读模式
英文:

MAUI: Global exception handling with error page or snack

问题

I'm trying the new .Net MAUI to build a personal 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 catches 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 crashes every time even if I use MarshalManagedException or any other thing I've found on the web (I've used all solutions posted on MAUI GitHub about Global Handling Exception). On Android and Windows, I can catch errors and set the handled to true to prevent the app from crashing.

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

Can someone help me? Or is it not possible on MAUI (I want to try another tech than Flutter).

英文:

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

得分: 4

"全局异常处理程序"在Maui中仅是记录导致应用崩溃的详细信息的机会,在应用关闭之前 - 这不是一种可恢复的状态。因为一旦代码到达那里,Maui不能保证安全地执行任何可显示的操作。

  • 为了避免应用崩溃,必须将所有内容包装在try..catch中。这意味着您的代码获得控制的每个地方。每个按钮处理方法等。

即使在那里也有崩溃Maui内部或平台图形的方法 - 但如果您已经对所有内容进行了尝试捕获,那些只是极端情况。

理论上,只有应用程序员可以决定是否可以安全地从某种情况中恢复,而不是关闭应用程序。try-catch为您提供了在每个代码位置分别决定的位置。

我确实希望Maui提供一种简单的方法来包装所有用户交互在单个异常处理程序中(可以恢复并继续)。但它没有。

(Maui的Blazor部分可能会为基于Web的代码提供这个。我对此不太熟悉。)

英文:

"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.)

huangapple
  • 本文由 发表于 2023年2月24日 03:43:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定