0

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

krckumar
  • 544
  • 4
  • 21
  • Does this https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Application#Windows_UI_Xaml_Application_UnhandledException work for you? – Nick Jan 22 '18 at 15:18

2 Answers2

3

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.

evictednoise
  • 587
  • 1
  • 7
  • 19
  • Thank you so much for your response. This is exactly the same one I am looking for. But I was looking for an example on how to use the same from my app. 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. https://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception(v=vs.110).aspx An equivalent of the above documented example will be very helpful for me. – krckumar Jan 23 '18 at 08:46
  • Xavier beat me to it.. Check his answer below :) – evictednoise Jan 23 '18 at 09:48
1

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)

Xie Steven
  • 8,544
  • 1
  • 9
  • 23