標籤:
try...catch語句將能引發錯誤的代碼放在try塊中,並且對應一個相應,然後有異常被拋出。
文法
try { try_statements}[catch (exception_var_1 if condition_1) { // non-standard catch_statements_1}]...[catch (exception_var_2) { catch_statements_2}][finally { finally_statements}]
-
try_statements
-
需要被執行的語句。
-
catch_statements_1, catch_statements_2
-
如果在try塊裡有異常被拋出時執行的語句。
-
exception_var_1, exception_var_2
-
一個用來儲存拋出對象的變數根據相關catch子句。
-
condition_1
-
一個條件運算式。
-
finally_statements
-
在try語句塊之後執行的語句塊.無論是否有異常拋出或捕獲這些語句都將執行.
描述
try語句包含了由一個或者多個語句組成的try塊, 和至少一個catch子句或者一個finally子句的其中一個,或者兩個兼有, 下面是三種形式的try聲明:
-
try...catchtry...finallytry...catch...finally
catch子句中包含特定在try塊中拋出異常是執行的語句.也就是,你想試一試這個try語句是否成功,如果不成功你將去控制通過catch塊。 如果有在try塊中有任何一個語句(或者從try塊中調用的函數)拋出異常, 控制立即轉向catch子句。 如果在try塊中沒有異常拋出,這catch子句將會跳過。
finally子句在try塊和catch塊之後執行但是在下一個try聲明之前執行. 無論是否有異常拋出或捕獲它總是執行。
你可以嵌套一個或者更多的try語句。 如果內部的try語句沒有catch子句,那麼將會進入包圍它的try語句的catch子句。
你也可以用try語句去處理javascript異常. 參考 JavaScript Guide 瞭解更多關於Javascript異常的資訊。
無條件的catch子句
當單個的無條件的catch 子句被使用時, 當有異常拋出時則將會進入catch塊執行語句. 例如, 當下面的代碼發生異常時,控制將會轉向catch子句。
try { throw "myException"; // generates an exception}catch (e) { // statements to handle any exceptions logMyErrors(e); // pass exception object to error handler}
條件catch子句
非標準
該特性是非標準的,請盡量不要在生產環境中使用它!
你也可以用一個或者更多條件catch子句來處理特定的異常。在這種情況下,當異常拋出時將會進入合適的catch子句中。 在下面的代碼中,try塊的代碼可能會拋出三種異常: TypeError, RangeError, 和EvalError。當一個異常拋出時,控制將會進入與其對應的catch語句。如果這個異常不是特定的,那麼控制將轉移到非條件catch子句。
當用一個非條件catch子句和一個或多個條件陳述式時,非條件catch子句必須放在最後。否則當到達條件陳述式之前所有的異常將會被非條件陳述式攔截。
提醒:這個功能不符合ECMAscript規範。
try { myroutine(); // may throw three types of exceptions} catch (e if e instanceof TypeError) { // statements to handle TypeError exceptions} catch (e if e instanceof RangeError) { // statements to handle RangeError exceptions} catch (e if e instanceof EvalError) { // statements to handle EvalError exceptions} catch (e) { // statements to handle any unspecified exceptions logMyErrors(e); // pass exception object to error handler}
下面用符合ECMAscript規範的簡單的JavaScript來編寫相同的“條件catch子句”(顯然更加冗長的,但是可以在任何地方運行):
try { myroutine(); // may throw three types of exceptions} catch (e) { if (e instanceof TypeError) { // statements to handle TypeError exceptions } else if (e instanceof RangeError) { // statements to handle RangeError exceptions } else if (e instanceof EvalError) { // statements to handle EvalError exceptions } else { // statements to handle any unspecified exceptions logMyErrors(e); // pass exception object to error handler }}
異常標識符
當try塊中的拋出一個異常時, exception_var (e.g. the e in catch (e)) 用來儲存被拋出聲明指定的值。 你可以用這個標識符來擷取關於被拋出異常的資訊。
這個標識符是catch子語句內部的。 換言之, 當進入catch子語句時標識符建立,catch子語句執行完畢後,這個表示符將不再可用。
finally子句
finally子句在try塊和catch塊之後執行但是在一個try聲明之前執行. 無論是否有異常拋出或捕獲它總是執行。 如果有異常拋出finally子句將會被執行,這個聲明即使沒有catch子句處理異常。
你可以用finally子句使你的指令碼結束更加優雅; 例如, 你可能需要釋放你的指令碼已經用過的資源。下面的樣本開啟一個檔案然後執行使用檔案的語句(伺服器端的javaScript允許你訪問檔案)。如果當檔案開啟時有一個異常拋出, finally子句關閉檔案在指令碼結束之前。
openMyFile()try { // tie up a resource writeMyFile(theData);}finally { closeMyFile(); // always close the resource}
例子嵌套 try-blocks
首先讓我們看看這裡發生什麼:
try { try { throw new Error("oops"); } finally { console.log("finally"); }}catch (ex) { console.error("outer", ex.message);}// Output:// "finally"// "outer" "oops"
現在,如果我們已經捕獲異常通過增加一個catch語句塊
try { try { throw new Error("oops"); } catch (ex) { console.error("inner", ex.message); } finally { console.log("finally"); }}catch (ex) { console.error("outer", ex.message);}// Output:// "inner" "oops"// "finally"
現在,讓我們再次拋出錯誤。
try { try { throw new Error("oops"); } catch (ex) { console.error("inner", ex.message); throw ex; } finally { console.log("finally"); }}catch (ex) { console.error("outer", ex.message);}// Output:// "inner" "oops"// "finally"// "outer" "oops"
任何給定的異常只會被離它最近的封閉catch塊捕獲一次。當然,在“內部”塊拋出的任何新異常 (因為catch塊裡的代碼也可以拋出異常),將會被“外部”塊所捕獲。
從finally塊返回
如果從finally塊中返回一個值,那麼這個值將會成為整個try-catch-finally的傳回值,無論是否有return語句在try和catch中。這包括在catch塊裡拋出的異常。
try { try { throw new Error("oops"); } catch (ex) { console.error("inner", ex.message); throw ex; } finally { console.log("finally"); return; }}catch (ex) { console.error("outer", ex.message);}// Output:// "inner" "oops"// "finally"
因為finally塊裡的return語句,外部的“oops”異常沒有拋出。從catch塊返回的值同樣適用。
規範
| Specification |
Status |
Comment |
| ECMAScript 3rd Edition |
Standard |
Initial definition.
Implemented in JavaScript 1.4 |
ECMAScript 5.1 (ECMA-262) try statement |
Standard |
|
ECMAScript 6 (ECMA-262) try statement |
Release Candidate |
|
Not part of an ECMA-262 standard: Multiple catch clauses and conditional clauses (SpiderMonkey extension, JavaScript 1.5).
瀏覽器安全色性
| Feature |
Chrome |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
| Basic support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Conditional clauses
(non-standard) |
未實現 |
(Yes) |
未實現 |
未實現 |
未實現 |
參見
javascript try...catch語句