Ale*_*lex 5 javascript error-handling node.js
如果我像这样定义自定义错误类:
class MyCustom Error extends Error{ }
Run Code Online (Sandbox Code Playgroud)
我怎样才能捕捉到多个这样的错误:
try{
if(something)
throw MyCustomError();
if(something_else)
throw Error('lalala');
}catch(MyCustomError err){
}catch(err){
}
Run Code Online (Sandbox Code Playgroud)
?
上面的代码不起作用并给出一些语法错误
MDN文档if/else建议在语句内使用块catch。这是因为不可能有多个catch语句,并且您无法以这种方式捕获特定错误。
try {
myroutine(); // may throw three types of exceptions
} catch (e) {
if (e instanceof TypeError) {
// statements to handle TypeError exceptions
} else if (e instanceof RangeError) {
// statements to handle RangeError exceptions
} else if (e instanceof EvalError) {
// statements to handle EvalError exceptions
} else {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5602 次 |
| 最近记录: |