在处理当前项目时,我看到了一些奇怪的代码构造,如下所示:
async_api_call_with_throw_errors //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.then(() => async_call_with_throw_errors) //pseudocode
.catch((e) => {
Sentry.captureException(e)
if (e?.validateError) {
//some actions
} else {
dispatch({ type: errorOccurred, errorText: e?.message, fatalError: false })
}
})
.catch(e => {
Sentry.captureException(e)
dispatch({ type: errorOccurred, errorText: e, fatalError: false })
})
如果“ catch”块捕获了几种错误类型,那是可以的,但是“ then”块之后的两个具有相同错误的“ catch”块有意义吗?
我觉得不是。但是也许我对try ... catch不了解重要的事情?
如果前一个捕获引发错误,然后被后续捕获捕获,则具有多个捕获可以正常工作。看下面的代码。
const promise1 = new Promise((resolve, reject) => {
throw 'Uh-oh!';
});
promise1.catch((error) => {
console.error(error);
throw "Error was caught!"
}).catch(err => {
console.error(err)
})
// Expect Output:
// > Uh-oh!
// > Error was caught!
希望这能消除一些疑问。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句