How to try/catch all exceptions

2019-07-01 17:42发布

I'm completing a UWP app started by someone else. The app crashes frequently and I always end up in App.g.i.cs at

if (global::System.Diagnostics.Debugger.IsAttached)
    global::System.Diagnostics.Debugger.Break(); 

where I then have to say "no, don't start the debugger" and close 2 windows.

Is there somewhere I could put a big try/catch so that I don't have to restart the app each time this happen? I can't find anything in AppShell or App.

Or do I have to put a try/catch in every single event handler?

4条回答
小情绪 Triste *
2楼-- · 2019-07-01 18:24

You can try/catch in your main app to catch all exceptions. Shown is an example using Xamarin Forms DisplayAlert:

            try
            {
                //throw new Exception("gone and done it");
                MyMainProgram();
                                }
            catch (Exception ex)
            {
                await DisplayAlert("Whoops!", ex.Message, "ok");
                throw ex;
            }

You can test this by uncommenting the "throw new Exception". Execution stops with an alert you must answer, then continues by throwing the exception to prevent executing corrupted code.

查看更多
Anthone
3楼-- · 2019-07-01 18:27

If you want to treat the cause rather than the symptoms, you should enable first-chance exceptions in the debugger settings. Then the debugger should break at the source of the exception, rather than in the global handler. You can then address the root cause of the problems directly.

Shipping an app that has the global handler blindly set every exception as "handled" even when you don't know why it's failing is not a good solution.

查看更多
聊天终结者
4楼-- · 2019-07-01 18:40

If you want to avoid starting the new debugger and restarting the app each time when encountering unhandled exceptions, you can use Application.UnhandledException event and set the Handled property of the event arguments to true like following:

public App()
{
    this.InitializeComponent();
    this.Suspending += OnSuspending;
    this.UnhandledException += (sender, e) =>
    {
        e.Handled = true;
        System.Diagnostics.Debug.WriteLine(e.Exception);
    };
}

The UnhandledException event is used to notify the app about exceptions encountered by the XAML framework or by the Windows Runtime in general that have not been handled by app code.

Normally after the UnhandledException event is fired, the Windows Runtime terminates the app because the exception was unhandled. The app code has some control over this: if the UnhandledException event handler sets the Handled property of the event arguments to true, then in most cases the app will not be terminated.

For more info, please see Remarks of Application.UnhandledException event and the blog: Strategies for Handling Errors in your Windows Store Apps.

查看更多
一夜七次
5楼-- · 2019-07-01 18:42

As far as i know most you cant do what you are trying to do (big try catch block) and for all intents and purposes you shouldn't even consider that possibility.

First try to determine why the app is crashing, on what page, is it when you try to something specific, the same thing everytime and then you can try catch some of the methods on that particular page and determine what's causing the crash

查看更多
登录 后发表回答