javascript try catch语句在我的代码中不起作用 我得到了“未捕获的SyntaxError:无效或意外的令牌”错误
试着用“try~catch”来接球,但它不起作用javascript try catch语句在我的代码中不起作用 我得到了“未捕获的SyntaxError:无效或意外的令牌”错误,javascript,error-handling,try-catch,uncaughtexceptionhandler,Javascript,Error Handling,Try Catch,Uncaughtexceptionhandler,试着用“try~catch”来接球,但它不起作用 function a(){ try{ var img1 = "==""; <#-- it occurs error --> }catch (e) { console.log("image error:" + e.message); } } 函数a(){ 试一试{ var img1=“=”; }捕获(e){ console.log(“图像错误:+e.message”); } } 您有语法错误,无法捕获语法
function a(){
try{
var img1 = "==""; <#-- it occurs error -->
}catch (e) {
console.log("image error:" + e.message);
}
}
函数a(){
试一试{
var img1=“=”;
}捕获(e){
console.log(“图像错误:+e.message”);
}
}
您有语法错误,无法捕获语法错误 在解析或验证代码时会检查此错误。
try..catch
语句在运行时执行。因此,它无法正常工作
如果您评估或解析(例如JSON)代码,您可以只处理语法错误,,也可以创建如下语法错误:
try {
throw new SyntaxError('Hello', 'someFile.js', 18);
} catch (e) {
console.log(e.message); // "Hello"
console.log(e.name); // "SyntaxError"
}
对于eval
处理的语法错误或自创语法错误:
当JavaScript引擎在解析代码时遇到不符合语言语法的标记或标记顺序时,将抛出SyntaxError
从
试试这个:
function a(){
try{
var img1 = "==\"";
//but you have to put some error here without parsing or validation error of your code.
}catch (e) {
console.log("image error:" + e.message);
}
}
我建议您阅读:
try..catch
,因为语法错误中断了脚本。可能重复