我有一个try / catch块,我已经写入了JetBrains Webstorm IDE,它给了我一个错误。错误内容如下:“本地捕获异常'抛出'/此检查报告任何JavaScript 抛出语句的实例,其异常总是通过包含 try 语句来捕获。使用< strong> throw 语句作为'goto'来改变本地控制流可能会令人困惑。“
try {
var invoice = parseInt(localStorage[0]);
if (isNaN(invoice)) {
console.warn("invoice NaN; let's fix that...");
throw "executing catch";
}
}
catch (e) {
console.log(e);
this.test();
invoice = 1;
}
finally {
localStorage[invoice] = JSON.stringify(Ticket);
console.log("localStorage[" + invoice + '] : ' + localStorage[invoice]);
localStorage[0] = parseInt(localStorage[0]) + 1;
}
如果我将来自catch块的活动放入try块的if块,那么我将不需要try / catch / finally。那么除了使用条件语句(if)之外,你还会抛出一个错误,以及如何避免将throw用作“goto”?
谢谢!
答案 0 :(得分:0)
这个功能相同,更清洁。为什么要抛出/抓住?
try {
var invoice = parseInt(localStorage[0]);
if(isNaN(invoice)){
console.warn("invoice NaN; let's fix that...");
console.log("executing catch"); // not really needed
this.test();
invoice = 1;
}
} finally {
localStorage[invoice] = JSON.stringify(Ticket);
console.log("localStorage[" + invoice + '] : ' + localStorage[invoice]);
localStorage[0] = parseInt(localStorage[0]) + 1;
}