【发布时间】:2020-12-11 00:42:04
【问题描述】:
例子
try {
let items = JSON.parse(localStorage.getItem('items'))
if(items.length === 0){
throw new Exception()
}
// other code
fetch('http://localhost:9004/merge-items').then(...)
}catch(e){
console.error(e)
fetch('http://localhost:9004/load-items').then(...)
}
我在 if 块内抛出异常,因为我需要编写与 catch 块内类似的代码
try {
let items = JSON.parse(localStorage.getItem('items'))
if(items.length === 0){
fetch('http://localhost:9004/load-items').then(...) // The same code
}
// other code
fetch('http://localhost:9004/merge-items').then(...)
}catch(e){
console.error(e)
fetch('http://localhost:9004/load-items').then(...) // The same code
}
【问题讨论】:
-
欢迎来到 Stack Overflow!请拿起tour(你得到一个徽章!),环顾四周,通读help center,特别是How do I ask a good question?、What types of questions should I avoid asking?和What topics can I ask about here? “可以吗?” 征求意见,这与 SO 无关。
标签: javascript exception try-catch