1

Can anybody tell me how to deal with unhandled exceptions in WinJS code. Is there are better way to handle them instead of using try/catch block.I have already made use of try/catch block in some parts of my code.

4 Answers 4

3

try/catch is the language mechanism to handle exceptions.

Are you dealing with regular exceptions, or do you have an unhandled exception in asynchronous code (inside a promise)? If the latter, try/catch won't work, because the stack frame that set up the try/catch is gone by the time the async operation completes.

In that case, you need to add an error handler to your promise:

doSomethingAsync().then(
    function (result) { /* successful completion code here */ },
    function (err) { /* exception handler here */ });

Exceptions propagate along promise chains, so you can put a single handler at the end and it'll handle any exceptions within that promise chain. You can also pass an error handler to the done() method. The result could look something like this:

doSomethingAsync()
    .then(function (result) { return somethingElseAsync(); })
    .then(function (result) { return aThirdAsyncThing(); })
    .done(
        function (result) { doThisWhenAllDone(); },
        function (err) { ohNoSomethingWentWrong(err); }
    );

Finally, unhandled exceptions eventually end up at window.onerror, so you could capture them there. I would only do logging at this point; trying to recover your app and keep running from a top-level error handler is generally a bad idea.

2

I think you are asking for the equivalent to an ASP.NET Webforms Application_Error catchall. The WinJS equivalent to ASP.NET's Application_Error method is WinJS.Application.onerror.

The best way to use this, is in your default.js file (or similar) and add a listener like:

    WinJS.Application.onerror = function(eventInfo){

            var error = eventInfo.detail.error;
    //Log error somewhere  using error.message and error.description..

  // Maybe even display a Windows.UI.Popups.MessageDialog or Flyout or for all unhandled exceptions


    };

This will allow you to capture all unhandled exceptions that appear in the application gracefully.

1
  • @TomMcKearney You are correct. You'd have to log the error and then have another process display the error information.
    – techsaint
    Aug 10, 2014 at 19:11
1

Here's an alternative to all these solutions that I had to discover on my own. Whenever you use promise objects, make sure your done() call handles both success and error situations. If you don't handle failures, the system will end up throwing an exception that won't be caught either by try/catch blocks nor via the usual WinJS.Application.onerror methods.

This issue has cost me 2 Windows Store rejections already before I discovered it on my own... :(

0

link: How-To: Last Chance Exception Handling in WinJS Applications

All you really need to do to treat unhandled exceptions is hook into the WinJS Application.onerror event, like this (from a default.js file:)

(function () {
"use strict";

var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
var nav = WinJS.Navigation;
WinJS.strictProcessing();

app.onerror = function (error) {
    //Log the last-chance exception (as a crash)
    MK.logLastChanceException(error);
};

/* rest of the WinJS app event handlers and methods */

app.start();})();

Bear in mind that you have no access to network IO when the WinRT is crashing, but you can write to disk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.