生成一个可由 try...catch...finally 语句处理的错误条件。
throw [exception]
- exception
可选项。任何表达式。
使用 throw 语句时可不带参数,但是只有在 throw 语句包含在 catch 块中时才可以这样做。在这种情况下,throw 语句会再次引发由封闭 catch 语句捕获的错误。提供参数时,throw 语句会引发 exception 的值。
示例下面的示例基于传入的值引发一个错误,然后阐释了在 try...catch...finally 语句层次中如何处理该错误:
function TryCatchDemo(x){ try { try { if (x == 0) // Evalute argument. throw "x equals zero"; // Throw an error. else throw "x does not equal zero"; // Throw a different error. } catch(e) { // Handle "x=0" errors here. if (e == "x equals zero") // Check for a handled error. return(e + " handled locally."); // Return error message. else // Can't handle error here. throw e; // Rethrow the error for next } // error handler. } catch(e) { // Handle other errors here. return(e + " error handled higher up."); // Return error message. } } print(TryCatchDemo(0)+ "\n"); print(TryCatchDemo(1));要求请参见
参考
try...catch...finally 语句错误对象