简体   繁体   中英

Equivalent of UncaughtExceptionHandler of java in C# for UWP Apps

In java we have Thread.UncaughtExceptionHandler for handling the unhandled exceptions. How can I handle the unhandleded exception in C# UWP.

I am unable to use the AppDomain as it is not supported. Also, there are some references around Assemblies. Is there any example or document I can refer to ?

Thank you

Maybe you are looking for UnhandledException

You can subscribe the OnUnhandledException event and 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.

Since I am new with C#, I am finding it hard to implement using the details in that documentation. I found one link as follows, but that is not usable for UWP. msdn.microsoft.com/en-us/library/… An equivalent of the above documented example will be very helpful for me.

If you create a new blank UWP project, you will find the App.xaml.cs file. Open it and register the UnhandledException event for your app like the following:

sealed partial class App : Application
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();
        this.Suspending += OnSuspending;
        this.UnhandledException += App_UnhandledException;
    }

    private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        //TODO:
    }
....
}

在此处输入图片说明

Since you said that you're new with c#, what you need to do is to learn some c# basic technology at first. For example, How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM