Can syntax errors be caught in JavaScript?

前端 未结 5 1242
说谎
说谎 2020-12-06 09:38

MDN states:

A SyntaxError is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language wh

5条回答
  •  遥遥无期
    2020-12-06 10:37

    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).

    I'd recommend you read these:

    • https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Statements#try...catch_Statement

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

提交回复
热议问题