WinJS中未处理的异常

有谁能告诉我如何处理WinJS代码中未处理的异常。是否有更好的方式来处理它们而不是使用try/catch块。我已经在我的代码的某些部分中使用了try/catch块。WinJS中未处理的异常

回答:

try/catch是处理异常的语言机制。

您是否正在处理常规异常,或者您在异步代码(承诺内)中是否有未处理的异常?如果是后者,则try/catch将不起作用,因为设置try/catch的堆栈帧在异步操作完成时消失。

在这种情况下,你需要一个错误处理程序添加到您的承诺:

doSomethingAsync().then(

function (result) { /* successful completion code here */ },

function (err) { /* exception handler here */ });

例外沿着承诺链传播,所以你可以放在最后一个处理程序,它会处理中的任何异常那承诺链。您也可以将错误处理程序传递给done()方法。其结果可能是这个样子:

doSomethingAsync() 

.then(function (result) { return somethingElseAsync(); })

.then(function (result) { return aThirdAsyncThing(); })

.done(

function (result) { doThisWhenAllDone(); },

function (err) { ohNoSomethingWentWrong(err); }

);

最后,未处理的异常最终在window.onerror结束了,所以你可以捕捉到他们那里。我现在只做日志记录;试图恢复你的应用程序,并继续从顶级错误处理程序运行通常是一个坏主意。

回答:

我想你是要求相当于一个ASP.NET Webforms Application_Error catchall。相当于ASP.NET的Application_Error方法的WinJS是WinJS.Application.onerror。

使用最好的方法,就是在你的default.js文件(或类似),并添加一个侦听器,如:

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

};

这将让您拍摄摆好出现在应用程序的所有未处理的异常。

回答:

链接:How-To: Last Chance Exception Handling in WinJS Applications

你真正需要做的治疗未处理的异常是挂接到WinJS Application.onerror事件,像这样(从default.js文件:记住

(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();})();

熊说当WinRT崩溃时,您无法访问网络IO,但可以写入磁盘。

回答:

下面是我不得不亲自发现的所有这些解决方案的替代方案。无论何时使用promise对象,都要确保done()调用同时处理成功和错误情况。如果你不处理失败,那么系统最终会抛出一个不会被try/catch块或通过通常的WinJS.Application.onerror方法捕获的异常。

这个问题之前,我发现它在我自己花了我2个Windows应用商店的拒绝已经... :(

以上是 WinJS中未处理的异常 的全部内容, 来源链接: utcz.com/qa/262147.html

回到顶部