可以在JavaScript中捕获语法错误吗? [英] Can syntax errors be caught in JavaScript?

查看:141
本文介绍了可以在JavaScript中捕获语法错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MDN状态


当JavaScript引擎在解析代码时遇到不符合语言语法的令牌或令牌顺序时,抛出SyntaxError。

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.

但如果出现语法错误,程序怎么可能首先运行?

But if there's a syntax error, how could the program even run in the first place?

如何捕获JavaScript语法错误?

How can JavaScript syntax errors even be caught?

推荐答案

这是可以通过try-catch捕获的运行时错误,而不是语法错误(,如果你 eval 你的代码你可以处理恶意代码中的语法错误,但这很奇怪)。

It's runtime errors that can be caught with try-catch, not syntax errors (if you eval your code you can handle syntax errors in the evaled code but that's just weird).

我建议你阅读这些:

https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Statements#Exception_Handling_Statements

这篇关于可以在JavaScript中捕获语法错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆