我可以在 Javascript 中抛出异常,从而停止 Javascript 执行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9431434/
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
Can I throw an exception in Javascript, that stops Javascript execution?
提问by Mathias F
I try to simulate a problem where a script that is loaded from an external url stops execution of any more scripts on my site.
我尝试模拟一个问题,即从外部 url 加载的脚本停止在我的站点上执行任何更多脚本。
I tried to simulate such a problem by calling a function that does not exits. I can see the error in firebug but different scripts on the page are still executed.
我试图通过调用一个不退出的函数来模拟这样的问题。我可以在 firebug 中看到错误,但页面上的不同脚本仍在执行。
Are there different kinds of errors in Javascripts? If yes: what kind of error stops script execution? I only need this answer for Firefox.
Javascript 中是否存在不同类型的错误?如果是:什么样的错误会停止脚本执行?我只需要 Firefox 的这个答案。
EDIT
编辑
This question is easy to misunderstood but Rob W got it: I need to throw an exception and that exception needs to stop further script execution.
这个问题很容易被误解,但 Rob W 明白了:我需要抛出一个异常并且该异常需要停止进一步的脚本执行。
回答by Rob W
Answer to the title: No
Answer to "Are there different kinds of errors in JavaScript**: Yes, see MDN: Error
Syntax errors will prevent a whole script block from being executed, other errors (TypeErrors, Reference errors) will only stop the execution after the occurrence of the error.
对标题的 回答:No
Answer to "Are there different types of errors in JavaScript**: Yes, see MDN: Error
Syntax 错误将阻止整个脚本块被执行,其他错误(TypeErrors、Reference 错误)只会停止执行错误发生后执行。
Different <script>
blocks are executed separately. You cannot prevent the second block from execution by throwing an error in the first block (Demo: http://jsfiddle.net/WJCEN/).
不同的<script>
块分别执行。您不能通过在第一个块中抛出错误来阻止第二个块的执行(演示:http: //jsfiddle.net/WJCEN/)。
<script>Example: Syntax error in this script.</script>
<script>console.log('Still executed.')</script>
Also, if an error is caught using try-catch
(demo: http://jsfiddle.net/WJCEN/1/), then the error will not even stop the execution a whole block.
此外,如果使用try-catch
(demo: http://jsfiddle.net/WJCEN/1/)捕获错误,则该错误甚至不会停止整个块的执行。
try {throw 'Code';}catch(e){}
console.log('Still executed');
没有通用的万能方法可以阻止所有代码运行。对于单个脚本,您可以使用一些技巧来阻止代码进一步运行。
Example 1 (demo): Temporary overwrite a method
示例 1(演示):临时覆盖一个方法
1: <script>window._alert = alert;alert=null;</script>
2: <script>alert('Some code!');confirm('Not executing');</script>
3: <script>alert=_alert;delete window._alert;alert('Done!');</script>
This method is based on the fact that script 2 expects alert
to be a function. We have rewritten alert
to a non-function property (script 1). Script 2 throws a TypeError
, and the second block is skipped.
We restore the original values in script 3.
该方法基于脚本 2 期望alert
是函数这一事实。我们已重写alert
为非函数属性(脚本 1)。脚本 2 抛出 a TypeError
,并跳过第二个块。
我们恢复了脚本 3 中的原始值。
Example 2 (demo): Define a constant method, which cannot be overwritten.
示例 2(演示):定义一个常量方法,该方法不能被覆盖。
4. <script>Object.defineProperty(window, 'test',{value:null});</script>
5. <script>var test=function(){alert('Test');};test();alert('What?');</script>
This methods relies on the Object.defineProperty
method, to effectively define a constant value. In strict mode, the var test
declaration would throw a TypeError: "test is read-only".
When strict mode is not enables, a TypeError will be thrown at test()
: "test is not a function" (because we defined test
to be constant, in script 4).
这个方法依赖于Object.defineProperty
方法,来有效地定义一个常量值。在严格模式下,var test
声明会抛出 TypeError: "test is read-only"。
当严格模式未启用时,将在以下位置抛出 TypeError test()
:“test is not a function”(因为我们test
在脚本 4 中定义为常量)。
Note: The last method is not working correctly with function declarations (see bug #115452, Chrome 17)
注意:最后一种方法在函数声明中不能正常工作(参见错误 #115452,Chrome 17)
回答by Mouna Cheikhna
you can use the error object which support the following two properties:
您可以使用支持以下两个属性的错误对象:
name: The name of the error.
message: A description of the error.
for example to stop execution you can use : throw new Error("myError");
例如,要停止执行,您可以使用: throw new Error("myError");
Are there different kinds of errors in Javascripts?
Javascript 中是否存在不同类型的错误?
Besides the generic Error constructor, there are six other core errors in JavaScript:
除了通用的 Error 构造函数之外,JavaScript 中还有其他六个核心错误:
see heredetails on these errors.
请在此处查看有关这些错误的详细信息。
回答by Kunal Vashist
Use
try catch finally block
利用
try catch finally block
It will do the trick
它会做的伎俩