SyntaxError: Missing catch or finally after try



如何修复"SyntaxError:缺少catch或finally after try";

我试着做一个科学的计算器,我得到了这个错误

function maths(a) {
math = a;
try {
if (operatorSign =="+") {
prep();
firstI = first - second;
mathematics(); 
first = second + "+" + result;
} else if (operatorSign =="-") {
prep();
firstI = second - first;
mathematics();
first = second + "-" +"("+result+")";
} else if (operatorSign =="*") {
prep();
firstI = second / first;
mathematics();
first = second + "/" + result;
} else {
firstI = first;
mathematics();
first = result;
}
return first;
}
}

首先,阅读代码的作用并不容易。但是你需要一个接球盖帽后尝试盖帽。您可以在这里找到文档。就像在这个简短的例子中,函数nonExistentFunction不存在,因此代码将出现错误。这是抓块抓。catch块不需要做任何事情(就像我在控制台中打印错误一样(。

try {
nonExistentFunction();
} catch (error) {
console.error(error);
}

它或多或少是这样的:

try {
someFunction();
} catch(err) {
throw new Error(err);
} 

最新更新