有没有办法“白名单” - 或防止 - 在 try catch 块中抛出特定错误?
例如,假设我有一个我知道但不想记录的异常。但是,我想捕获该 try/catch 块中弹出的任何其他错误。
这是我能想到的最好的:
try {
...
} catch(exception) {
//Loop through a predefined list of known errors that we want to ignore.
for (var i in whitelistOfErrors) {
//if our exception MATCHES an error that we want to ignore,
//don't throw it
if (exception == whitelistOfErrors[i])
break;
//if our exception DOESN'T MATCH an error that we want to ignore,
//throw it
if (i = whitelistOfErrors.length)
throw exception;
}
}
(或者用为您搜索数组的方法替换逻辑,如果存在的话)