2013-03-14 49 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/