none
[UWP] Catching unexpected exception RRS feed

  • Question

  • I am trying to code global events handler (on app level).

    In my app code i have lines:

            public App()
            {
                UnhandledException += OnUnhandledException;
                InitializeComponent();
            }

            //Global exception handling
            private async void OnUnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
            {
                var messageDialog = new MessageDialog("Unhandled Exception");
                await messageDialog.ShowAsync();

                        e.Handled = true;
            }

    On the page level I have a button witch click event handler:

            private void btn2_Click(object sender, RoutedEventArgs e)
            {
                List<string> a = null ;
                var b = a.Count;

            }

    This code raises exception but it isn't caught on the app level.

    What I coded wrong?

    Friday, July 5, 2019 9:33 PM

Answers

  • Hello,

    You should put e.Handled = true; statement before any async call as  the code that rise UnhandledException  doesn't wait till your async task is finished and for its part e.Handled is false therefore terminate app


    Make the community better together

    • Marked as answer by BitSmithy Monday, July 15, 2019 3:56 PM
    Saturday, July 6, 2019 3:25 PM
  • Hi,

    Well, I test the code snippet. It works well. Is your code same as me?

    In app.xaml.cs

      private async void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
            {
                e.Handled = true;
    
                var messageDialog = new MessageDialog("Unhandled Exception");
                await messageDialog.ShowAsync();
    
            }

    in mainpage.cs

     private void Button_Click(object sender, RoutedEventArgs e)
            {
                List<string> a = null;
                var b = a.Count;
            }

    And what it looks like:

    When you are debugging your app and see the exception first, just click continue to make sure it will enter the unhandled exception handler.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BitSmithy Monday, July 15, 2019 3:56 PM
    Monday, July 8, 2019 2:28 AM
    Moderator

All replies

  • Hello,

    You should put e.Handled = true; statement before any async call as  the code that rise UnhandledException  doesn't wait till your async task is finished and for its part e.Handled is false therefore terminate app


    Make the community better together

    • Marked as answer by BitSmithy Monday, July 15, 2019 3:56 PM
    Saturday, July 6, 2019 3:25 PM
  • I tried your advice with below code - it doesnt work, exception is not caught, program terminates, dialog box is not showed.

    //Global exception handling

    private void OnUnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
    {

         e.Handled = true;

                var messageDialog = new MessageDialog("Unhandled Exception");
               messageDialog.ShowAsync();

    }


    • Edited by BitSmithy Saturday, July 6, 2019 8:01 PM
    Saturday, July 6, 2019 8:01 PM
  • Hi,

    Well, I test the code snippet. It works well. Is your code same as me?

    In app.xaml.cs

      private async void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
            {
                e.Handled = true;
    
                var messageDialog = new MessageDialog("Unhandled Exception");
                await messageDialog.ShowAsync();
    
            }

    in mainpage.cs

     private void Button_Click(object sender, RoutedEventArgs e)
            {
                List<string> a = null;
                var b = a.Count;
            }

    And what it looks like:

    When you are debugging your app and see the exception first, just click continue to make sure it will enter the unhandled exception handler.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by BitSmithy Monday, July 15, 2019 3:56 PM
    Monday, July 8, 2019 2:28 AM
    Moderator
  • >> When you are debugging your app and see the exception first, just click continue to make sure it will enter the unhandled exception handler.

    It is a key to my problem

    Monday, July 15, 2019 3:56 PM
  • It is part of the answer
    Monday, July 15, 2019 3:56 PM