Global exception handling in Windows Store JavaScript applications

When an unhandled exception occurs in a Windows Store app, this application is terminated immediately, in a silent way and without showing any error message to the user. This is the default behavior, but we can change it, if we want to.

The object WinJS.Application has a property called onerror, where we can define a function that will be executed every time an unhandled exception occurs in the application, allowing us to get details about the exception, do other things like write to a log, send an e-mail to the developer, etc…, and also allowing us to prevent the application from being terminated, if we signal that the exception was properly handled by our function. Think of it like a global trycatch for the entire JavaScript application.

WinJS.Application.onerror = function (e) {

    // The first argument give us details of the exception:
    var message = e.detail.message;
    var description = e.detail.description;
    var code = e.detail.number;
    var stackTrace = e.detail.stack;

    // ...

    // By returning true, we signal that the exception was handled,
    // preventing the application from being terminated
    return true;

As you can see in the code above, by returning true from the function assigned to the event onerror, we signal that the exception was handled and that the application shouldn’t be terminated. By returning false or just by not returning anything (which makes the function to return undefined), the application will be terminated as it would happen by default, but we had the chance to examine the exception and do something (e.g. log) before the application gets terminated.

One thing that I like to do when I’m developing apps, is to view unhandled exceptions in a dialog box as soon as they occur, during the execution of the application. Something more or less like this*:

WinJS.Application.onerror = function (e) {

    var dialog = new Windows.UI.Popups.MessageDialog(
        e.detail.stack, e.detail.message);


    return true;

Thus, whenever an unhandled exception occurs, I immediately see a dialog box with the details of the exception, and I can also continue to test other aspects of the application, hence it doesn’t get terminated because of the exception that just occurred.

* The “more or less like this” is due to the fact that I do a few more steps to make sure that these dialog boxes appear only during development, when I’m running a Debug build of the application, but that is a topic for another post.

This entry was posted in JavaScript, Windows 8 and tagged , , , , . Bookmark the permalink.

One Response to Global exception handling in Windows Store JavaScript applications

  1. Phil says:

    e.detail.stack is undefined in my Windows 8.1, WinJS app.
    Can you tell me how to get the stack trace out of the err object.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>