2013-03-14 58 views
0

我是JavaScript新手,但对编程/脚本一般来说并不陌生,而且我有关于throw例外的查询。是否有抛出异常的其他语句?

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

E.g

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

是类似的东西可能吗?

+2

[全部](http://jsbin.com/oxitab/1/edit) – C5H8NNaO4 2013-03-14 10:59:59

+0

什么finally块? – 2013-03-14 11:01:34

回答

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/