Javascript 中的错误继续下一步?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2978291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:46:06  来源:igfitidea点击:

On Error Resume Next in Javascript?

javascriptexception-handling

提问by Majid Fouladpour

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

是否try ... catch(e)提供与On Error Resume NextVB 中相同的服务?

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.

我有一个页面,它使用了几个 JQuery 插件以及我自己编写的一些函数。解决所有可能的异常需要大量的工作。

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?

现在,我想告诉脚本不要中断将是致命的错误。当我使用插件时我该怎么做?

回答by roufamatic

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

是的,try/catch提供了一种捕获错误的方法,尽管与 On Error Resume Next 不同,您选择处理catch块中的错误或根本不处理。

So in VB you might have done:

所以在 VB 中你可能已经完成了:

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:

在 JS 中,您将执行以下操作:

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!

当然,空的 catch 块是邪恶的,所以不要把它们留在生产代码中 yadda yadda。最好的办法是让错误发生并修复它们。快速失败!

回答by Bala

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

据我所知,javascript 中没有 ON ERROR RESUME NEXT,但以下模型将解决您的要求

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