To handle otherwise unhandled exceptions at the application level, you can use the window.onerror event or the WinJS.Application.onerror event.

The key difference between the two is that WinJS.Application.onerror handles only exceptions that occur within WinJS’s own event dispatching mechanism. This includes any exceptions that happen within asynchronous operations, as those involve promises and promises employ that dispatching mechanism. So that handler will see exceptions that come out of WinRT and other parts of WinJS. It won’t see errors that arise from elsewhere, however, such as if you’re using XmlHttpRequest directly instead of WinJS.xhr, or you throw an exception somewhere else in your code. Those you can only capture in window.onerror.

If you have both handlers in place, WinJS.Application.onerror will see any unhandled WinJS/WinRT exceptions first. If you don’t handle them there, they’ll go onto window.onerror.

Either way, it’s a good idea to have such handler in place in your debug build, as you can then catch any unhandled exception before you get to the ominous chunk of code in WinJS (terminateAppHandler) that calles MSApp.terminateApp().

 


Comments are closed