JavaScript 嵌套尝试异常
JavaScript nested try exception
我正在阅读 JavaScript exceptions:
You can nest one or more try...catch statements. If an inner
try...catch statement does not have a catch block, the enclosing
try...catch statement's catch block is checked for a match.
我认为我理解正确并编写了如下测试代码:
try {
try {
throw "error";
}
} catch( e ) {
console.log( e );
}
但是出现这个错误:
Uncaught SyntaxError: Missing catch or finally after try
我知道它清楚地表明我遗漏了一个问题或最后但 JavaScript 文档说我的代码应该有效还是我误解了?
引用的文字具有误导性,因为它说 "if an inner try..catch doesn't have a catch block" 没有任何意义。它应该只是 "if an inner try
doesn't have...".
在 JavaScript 中,您不能单独拥有一个 try
;它 有 有 catch
、finally
,或两者都有。所以引用的场景是 try/catch
包含 try/finally
(不是另一个 try/catch
):
try {
try {
throw "error";
}
finally {
}
} catch( e ) {
console.log( e );
}
此处指定:(https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/try...catch)
The try statement consists of a try block, which contains one or more
statements, and at least one catch clause or a finally clause, or
both. That is, there are three forms of the try statement:
我正在阅读 JavaScript exceptions:
You can nest one or more try...catch statements. If an inner try...catch statement does not have a catch block, the enclosing try...catch statement's catch block is checked for a match.
我认为我理解正确并编写了如下测试代码:
try {
try {
throw "error";
}
} catch( e ) {
console.log( e );
}
但是出现这个错误:
Uncaught SyntaxError: Missing catch or finally after try
我知道它清楚地表明我遗漏了一个问题或最后但 JavaScript 文档说我的代码应该有效还是我误解了?
引用的文字具有误导性,因为它说 "if an inner try..catch doesn't have a catch block" 没有任何意义。它应该只是 "if an inner try
doesn't have...".
在 JavaScript 中,您不能单独拥有一个 try
;它 有 有 catch
、finally
,或两者都有。所以引用的场景是 try/catch
包含 try/finally
(不是另一个 try/catch
):
try {
try {
throw "error";
}
finally {
}
} catch( e ) {
console.log( e );
}
此处指定:(https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/try...catch)
The try statement consists of a try block, which contains one or more statements, and at least one catch clause or a finally clause, or both. That is, there are three forms of the try statement: