有没有办法'白名单' - 或者防止 - 在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;
}
}
(或者用一种为你搜索数组的方法替换逻辑,如果存在的话)