Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以防止抛出特定异常?_Javascript_Exception_Error Handling - Fatal编程技术网

Javascript 是否可以防止抛出特定异常?

Javascript 是否可以防止抛出特定异常?,javascript,exception,error-handling,Javascript,Exception,Error Handling,有没有一种方法可以“白名单”或防止在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) {

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

这里已经给出了答案: