JavaScript 可以捕获语法错误吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5963045/
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 syntax errors be caught in JavaScript?
提问by Pacerier
A SyntaxError is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.
当 JavaScript 引擎在解析代码时遇到不符合语言语法的标记或标记顺序时,会抛出 SyntaxError。
But if there's a syntax error, how could the program even run in the first place?
但是如果有语法错误,程序怎么可能首先运行呢?
How can JavaScript syntax errors even be caught?
JavaScript 语法错误是如何被发现的?
采纳答案by oberhamsi
It's runtime errors that can be caught with try-catch, not syntax errors (if you eval
your codeyou can handle syntax errors in the evaled code but that's just weird).
使用 try-catch 可以捕获运行时错误,而不是语法错误(如果您eval
的代码可以处理已评估代码中的语法错误,但这很奇怪)。
I'd recommend you read these:
我建议你阅读这些:
回答by rohithpr
You cannot use try-catch
blocks to handle syntax errors as they are thrown while the code is being parsed and not while it's running.
您不能使用try-catch
块来处理语法错误,因为它们是在解析代码而不是在运行时抛出的。
However you can use window.onerror
and figure out that there's an error. You must ensure that the onerror
function is defined in a separate script tag and not in the tag in which the error may be present!
但是,您可以使用window.onerror
并找出存在错误。您必须确保该onerror
函数是在单独的脚本标签中定义的,而不是在可能存在错误的标签中!
Eg:
例如:
This will not work, because the script is yet to start running when the error is thrown:
这将不起作用,因为在抛出错误时脚本尚未开始运行:
<script>
window.onerror = function (e) {
console.log('Error: ', e);
};
console.log('a'');
</script>
This will work:
这将起作用:
<script>
window.onerror = function (e) {
console.log('Error: ', e);
};
</script>
<script>
console.log('a'');
</script>
回答by cjn
In the JS world, SyntaxError CAN be a runtime exception. This can arise, for instance, when trying to parse a JSON response that isn't JSON format. The server can send back lots of types of responses, so if you send a HTML body response to your request that's expecting JSON in the body, you're going to get a SyntaxError
thrown in the JS. In such a case, you would get an error message that looks something like this: SyntaxError: JSON Parse error: Unrecognized token '<'
.
在 JS 世界中, SyntaxError 可以是运行时异常。例如,当尝试解析非 JSON 格式的 JSON 响应时,可能会出现这种情况。服务器可以发回许多类型的响应,因此如果您向您的请求发送 HTML 正文响应,而该请求在正文中需要 JSON,那么您将SyntaxError
在 JS 中得到一个抛出。在这种情况下,你会得到一个看起来像这样的错误消息:SyntaxError: JSON Parse error: Unrecognized token '<'
。
But there are other runtime SyntaxErrors you could get as well. Mozilla has a list of some here: SyntaxErrors for JSON parsing
但是您也可以获得其他运行时 SyntaxErrors。Mozilla 在此处列出了一些列表:SyntaxErrors for JSON parsing
You may want to catch these in your code. You can do so with a generic try/catch block like this:
您可能希望在代码中捕获这些。您可以使用像这样的通用 try/catch 块来做到这一点:
try {
JSON.parse('<html></html>');
} catch (e) {
console.log("I catch & handle all errors the same way.");
}
OR you can look for the SyntaxError specifically:
或者您可以专门查找 SyntaxError :
try {
JSON.parse('<html></html>');
} catch (e) {
if (e instanceof SyntaxError) {
console.log("I caught a pesky SyntaxError! I'll handle it specifically here.");
} else {
console.log("I caught an error, but it wasn't a SyntaxError. I handle all non-SyntaxErrors here.");
}
}
Mozilla has even more info on JS errors and handling them.
Mozilla 有更多关于 JS 错误和处理它们的信息。
回答by Shirgill Farhan
You can catch programmer-generated and runtime exceptionsbut you cannot catch JavaScript syntax errors,though you may handle them in some browsers using window.onerror.
您可以捕获程序员生成的异常和运行时异常,但无法捕获JavaScript 语法错误,尽管您可以在某些浏览器中使用window.onerror处理它们。
This is taken from the book JavaScript- The Complete Reference by Thomas-Powell
which I like very much. You can refer to the code examples in that book.
这是JavaScript- The Complete Reference by Thomas-Powell
我非常喜欢的一本书。您可以参考该书中的代码示例。