On Error Resume Next in Javascript?

折月煮酒 提交于 2019-12-05 21:13:22

问题


Does try ... catch(e) provide the same service as On Error Resume Next in VB?

I have a page which uses several JQuery plugins as well as some functions I have written myself. It would take a lot of work to address all possible exceptions.

For now, I want to tell the script not to break on would be fatal errors. How do I do that when I'm using plugins?


回答1:


Yes, try/catch provides a way to capture errors, though unlike On Error Resume Next you choose to deal with the error in the catch block or not at all.

So in VB you might have done:

on error resume next
DoSomethingUnsavory
if err.number <> 0 then ...
on error goto 0 ' you DO do this, right?

In JS you'd do the following:

try {
    doSomethingUnsavory();
}
catch (e) {
    // handle the unsavoriness if needed
}

Of course empty catch blocks are evil so don't leave them in production code yadda yadda. The best thing is to let the errors occur and fix them. Fail fast!




回答2:


According to my knowledge there is no ON ERROR RESUME NEXT in javascript, but the following model will solve your requirement

try
{
    var providerRateAvg = data.entry.gd$rating.average;
}
catch(e)
{}


来源:https://stackoverflow.com/questions/2978291/on-error-resume-next-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!