krckumar
krckumar

Reputation: 544

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

Upvotes: 0

Views: 472

Answers (2)

Xie Steven
Xie Steven

Reputation: 8611

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:
    }
....
}

enter image description here

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)

Upvotes: 1

evictednoise
evictednoise

Reputation: 593

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.

Upvotes: 3

Related Questions