locked
What is the correct way to catch all unhandled exceptions for a Metro app? RRS feed

  • Question

  • There doesn't seem to be any default reporting of unhandled exceptions for Metro apps in the current build (at least for my app, exceptions are being swallowed silently).  What is the recommended pattern for handling unhandled exceptions in a Metro app?  I have hooked the Application.UnhandledException event, but that doesn't seem to be called (at least in my cases which are probably on threads other than the UI given all the async...).

     

    Tuesday, September 27, 2011 2:24 PM

Answers

  • @DavidOrn - one of my first tips in my tips/tricks session from BUILD was about turning all exception handling on.  From the Debug menu, choose Exceptions and check all those checkboxes.  In addition it looks like you've already added the UnhandledException handler for those that will bubble up.

     


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    Tuesday, September 27, 2011 4:35 PM

All replies

  • @DavidOrn - one of my first tips in my tips/tricks session from BUILD was about turning all exception handling on.  From the Debug menu, choose Exceptions and check all those checkboxes.  In addition it looks like you've already added the UnhandledException handler for those that will bubble up.

     


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer
    Tuesday, September 27, 2011 4:35 PM
  • Are there guidelines for how to best surface errors to users?

    Monday, June 11, 2012 1:48 PM
  • ..the accepted answer is a development environment setting? I interpret the question as to concern production environment exception handling. In that case, I wonder the same. I can't find a way to handle unexpected exceptions on background threads.
    Monday, August 13, 2012 9:03 AM
  • In your reply, you say "for those that will bubble up".

    Is there any documentation on which ones those are, or how it might be best to handle them.

    I've tried hooking up UnhandledException, but can't seem to get it to fire ever in the RTM version of W8.

    Am I missing something? 

    For instance, I

    throw new Exception("boom!") in the CommandsRequested event handler, and it never gets there.  The same problem when I added the throw to the Launched handler.


    Anthony Wieser | Wieser Software Ltd | www.wieser-software.com

    Tuesday, September 4, 2012 12:46 PM
  • In your reply, you say "for those that will bubble up".

    Is there any documentation on which ones those are, or how it might be best to handle them.

    I've tried hooking up UnhandledException, but can't seem to get it to fire ever in the RTM version of W8.

    Am I missing something? 

    For instance, I

    throw new Exception("boom!") in the CommandsRequested event handler, and it never gets there.  The same problem when I added the throw to the Launched handler.

    I think the culprit often is the new async-await pattern, and that people haven't learned to use it properly. I've definitely fallen into several pitfalls. The CommandsRequested event handler - is that an async method? do you return a Task, and not void? Who invokes the event handler, and does that source method properly handle exceptions?

    For reference and an example, see this stack exchange question i posted a couple of weeks ago.

    Wednesday, September 5, 2012 10:59 AM
  • Well, it's an event handler. 

    I didn't declare my event handler as async and I certainly didn't await anything, but I guess the compiler might have made it that way anyway.  Certainly nothing awaits it in my code.  The handler is called when you pop up the Settings for the app (press Windows+I)

    http://www.jaylee.org/post/2011/10/17/WinRT-and-the-sugar-of-NET-event-handlers.aspx seems to indicate that things are no longer as they seemed.  I was used to MultiCastDelegate, but it appears something else is now going on, and it uses an EventRegistrationTokenTable


    Anthony Wieser | Wieser Software Ltd | www.wieser-software.com

    Wednesday, September 5, 2012 11:12 AM
  • Ok I see. Well, I'm wrapping all my event handlers in try-catch blocks. For Commands, I've written a base class implementing ICommand that always invokes the delegate in a try catch block so that my code isn't littered with this pattern all over the place.

    The question of what exceptions Application.UnhandledException handles, still remains though... 


    Wednesday, September 5, 2012 11:24 AM
  • Well, I just managed to catch an exception!

    Here's what I did:

           void ReviewPage_Loaded(object sender, RoutedEventArgs e)
            {
    
                DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
                dataTransferManager.DataRequested  += dataTransferManager_DataRequested;
                    
            }
    

    This code ran twice and threw an exception that was actually caught.

    The problem is, when I caught it, the StackTrace for the exception was empty.

    So I still can't report what went wrong.

     

    Anthony Wieser | Wieser Software Ltd | www.wieser-software.com

    Friday, September 7, 2012 1:25 PM