是否有用于抛出异常的else语句



我是JavaScript新手,但一般不熟悉编程/脚本,我有一个关于throw例外的查询。

所以,我想知道的是throw异常是否有一种else语句

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

这样的事情可能吗?

您可能正在寻找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/

最新更新