是否有抛出异常的else语句?

时间:2013-03-14 10:58:14

标签: javascript throw

我是JavaScript的新手,但对于编程/脚本编程并不陌生,我对throw个例外有疑问。

所以,我想知道的是else例外的throw语句。

E.g

// More code above
try
{
    if(i=="something") {
        throw "'i' equals 'something'."
    }
    else
    {
        throw "'i' doesn't equal 'something'."
    }
}
// More code here

这样的事情可能吗?

1 个答案:

答案 0 :(得分:2)

您可能正在寻找finally关键字

try
{
    if(i=="something") {
        throw "'i' equals 'something'."
    }
    else
    {
        throw "'i' doesn't equal 'something'."
    }
} catch( ex ) {
    // your exception handling code
} finally {
    // Statements that are executed after the try statement completes.
}

演示:http://jsfiddle.net/7y6Hz/