0

我是 JavaScript 新手,但一般来说对编程/脚本并不陌生,我有一个关于throw异常的查询。

所以,我想知道的是有一种例外else声明。throw

例如

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

这样的事情可能吗?

4

1 回答 1

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/

于 2013-03-14T11:00:35.363 回答