none
[UWP][C#] How can I handle an exception that is thrown in the loaded event of a User Control? RRS feed

  • Question

  • Example repo: https://github.com/groovykool/UserControlExceptions.git

    I can't handle an exception that is thrown in the loaded event of a User Control?

    I have a placed try/catch section in all appropriate places. 

    The exception can be handled in the control code, but not from the main page code.

    I can also catch it in the UnHandledException Event in the App code.

    How can I handle the exception in the mainpage code?

    Thanks,

    Tony

    Friday, November 8, 2019 9:26 PM

Answers

  • Hi,

    The exception happens in the UserControl so that the try/catch block in the MainPage can't handle it but the try/catch block in the UserControl can.

    If you want to handle the UserControl's exception in MainPage, a possible way is to raise an event in your UserControl and subscribe to the event in MainPage.

    First, you need to defined an event in your UserControl and raise it when you get the exception:

     //create event
            public event EventHandler<Exception> LoadFailed;
    
            public void RaiseLoadFailedEvent(Exception ex)
            {
                this.LoadFailed?.Invoke(this,ex);
            }
    
            //raise the event when exception happens
            private void ViewExcept_Loaded(object sender, RoutedEventArgs e)
            {
                try
                {
                    throw new Exception("Test exception was thrown");
                }
                catch (Exception ex)
                {
                    RaiseLoadFailedEvent(ex);
                }
                
            }

    Then in the MainPage, you will need to subscribe the event:

    <local:ViewExcept LoadFailed="ViewExcept_LoadFailed"/>
     private void ViewExcept_LoadFailed(object sender, Exception e)
            {
                Debug.WriteLine(e.Message);
            }

    Now, in the ViewExcept_LoadFailed method, you can get the exceptions thrown from the UserControl.


    Best regards,

    Roy


    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    • Marked as answer by Groovykool Tuesday, November 12, 2019 12:30 AM
    Monday, November 11, 2019 2:38 AM
    Moderator

All replies

  • Hi,

    The exception happens in the UserControl so that the try/catch block in the MainPage can't handle it but the try/catch block in the UserControl can.

    If you want to handle the UserControl's exception in MainPage, a possible way is to raise an event in your UserControl and subscribe to the event in MainPage.

    First, you need to defined an event in your UserControl and raise it when you get the exception:

     //create event
            public event EventHandler<Exception> LoadFailed;
    
            public void RaiseLoadFailedEvent(Exception ex)
            {
                this.LoadFailed?.Invoke(this,ex);
            }
    
            //raise the event when exception happens
            private void ViewExcept_Loaded(object sender, RoutedEventArgs e)
            {
                try
                {
                    throw new Exception("Test exception was thrown");
                }
                catch (Exception ex)
                {
                    RaiseLoadFailedEvent(ex);
                }
                
            }

    Then in the MainPage, you will need to subscribe the event:

    <local:ViewExcept LoadFailed="ViewExcept_LoadFailed"/>
     private void ViewExcept_LoadFailed(object sender, Exception e)
            {
                Debug.WriteLine(e.Message);
            }

    Now, in the ViewExcept_LoadFailed method, you can get the exceptions thrown from the UserControl.


    Best regards,

    Roy


    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    • Marked as answer by Groovykool Tuesday, November 12, 2019 12:30 AM
    Monday, November 11, 2019 2:38 AM
    Moderator
  • Yes that works well if I have the code for the UserControl.  However if the UserControl is provided by another and is in a library/nuget package I can't catch the exceptions.  I have seen a few UserControls like that.  I guess the assumption that exceptions can always be caught by the next higher level method is incorrect in this case.

    Thanks,

    Tony

    • Marked as answer by Groovykool Tuesday, November 12, 2019 12:30 AM
    • Unmarked as answer by Groovykool Tuesday, November 12, 2019 12:30 AM
    Monday, November 11, 2019 6:34 PM