条件catch子句 - 浏览器支持 [英] Conditional catch clauses - browsers support

查看:52
本文介绍了条件catch子句 - 浏览器支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪些浏览器支持有条件捕获条款

在MDN try ... catch 你可以找到条件捕获子句非标准功能

On MDN try...catch you can find Conditional catch clauses as Non-standard feature.

try {
    myroutine(); // may throw three exceptions
} catch (e if e instanceof TypeError) {
    // statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
    // statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
    // statements to handle EvalError exceptions
} catch (e) {
    // statements to handle any unspecified exceptions
    logMyErrors(e); // pass exception object to error handler
}




注意:此功能不是ECMAScript规范的一部分。

Note: This functionality is not part of the ECMAScript specification.

任何现代浏览器都支持它?

It's supported by any modern browsers?

谷歌Chrome控制台返回未捕获语法错误:如果

Google Chrome's console returned Uncaught SyntaxError: Unexpected token if

意外令牌或者我使用:

Or shoud I use:

try {
    myroutine(); // may throw three 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
    }
}


推荐答案

你应该使用后者,因为它适用于所有地方,引用MDN

You should use the later one, as it works everywhere, quoting MDN


这里是如何实现的相同的条件捕获子句
仅使用符合ECMAScript
规范的简单JavaScript(显然它更详细,但无处不在(我在Firefox,IE和Chrome上测试过)),我有:



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
    }
}

第一个 catch(e如果e instanceof ..)仅适用于Mozilla Firefox,会在chrome和IE上出错

The first one catch (e if e instanceof ..) only works with Mozilla Firefox and gives errors on chrome and IE

Chrome

Uncaught SyntaxError: Unexpected token if

IE

SCRIPT1006: Expected ')'

这篇关于条件catch子句 - 浏览器支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆