| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/try...catch | [Back] [Original] |
Get to know MDN better
try...catch try catch finally try catch finally
try {
nonExistentFunction();
} catch (error) {
console.error(error);
// Expected output: ReferenceError: nonExistentFunction is not defined
//
}
try {
tryStatements
} catch (exceptionVar) {
catchStatements
} finally {
finallyStatements
}
tryStatementscatchStatementstry
exceptionVar finallyStatements try...catch...finally
try try catch finally catch finally try
try...catchtry...finallytry...catch...finallytry doSomething(); // SyntaxError
catch (e) console.log(e);
catch try try try catch try catch
finally try...catch...finally
try try catch try catch
try JavaScript JavaScript JavaScript
try exceptionVar catch (e) e catch
try {
throw new TypeError("");
} catch ({ name, message }) {
console.log(name); // "TypeError"
console.log(message); // ""
}
try {
throw new TypeError("");
} catch ({ name, message }) {
var name; // SyntaxError: Identifier 'name' has already been declared
let message; // SyntaxError: Identifier 'message' has already been declared
}
try {
throw " Error ";
} catch (e) {
if (!(e instanceof Error)) {
e = new Error(e);
}
console.error(e.message);
}
function isValidJSON(text) {
try {
JSON.parse(text);
return true;
} catch {
return false;
}
}
finally try catch try...catch...finally finally
try...finally try throwreturnbreak continue try...catch...finally catch try...catch...finally try throw catch finally try catch returnthrowbreakcontinue finally try catch finally finally
finally finally finally returnthrowbreakcontinuefinally
catch catch try catch
try {
throw ""; //
} catch (e) {
//
logMyErrors(e); //
}
try...catch if...else if...else catch
try {
myroutine(); //
} catch (e) {
if (e instanceof TypeError) {
// TypeError
} else if (e instanceof RangeError) {
// RangeError
} else if (e instanceof EvalError) {
// EvalError
} else {
//
logMyErrors(e); //
}
}
try {
myRoutine();
} catch (e) {
if (e instanceof RangeError) {
//
} else {
throw e; //
}
}
Java
try {
myRoutine();
} catch (RangeError e) {
//
}
//
try {
try {
throw new Error("");
} finally {
console.log("finally");
}
} catch (ex) {
console.error("", ex.message);
}
//
// "finally"
// "" ""
try catch
try {
try {
throw new Error("");
} catch (ex) {
console.error("", ex.message);
} finally {
console.log("");
}
} catch (ex) {
console.error("", ex.message);
}
//
// "" ""
// ""
try {
try {
throw new Error("");
} catch (ex) {
console.error("", ex.message);
throw ex;
} finally {
console.log("");
}
} catch (ex) {
console.error("", ex.message);
}
//
// "" ""
// ""
// "" ""
catch catch catch
finally finally
openMyFile();
try {
//
writeMyFile(theData);
} finally {
closeMyFile(); //
//
}
try return finally finally
function safeWriteMyFile() {
openMyFile();
try {
return writeMyFile(theData); //
} finally {
closeMyFile(); //
// return
}
}
finally return try order.sort() finally finally finally return return
function doIt() {
const order = ["z"];
try {
order.push("try");
return order.sort(); // ztry
} finally {
order.push("finally");
return order;
}
}
doIt();
// ["try", "z", "finally"] ["finally", "try", "z"] ["try", "z"]
"catch" "finally"
function doIt() {
try {
throw "catch";
} finally {
return "finally";
}
}
doIt(); // finally
finally
| ECMAScript 2027 LanguageSpecification # sec-try-statement |
| Web Proxy Viewer | New URL | Original Page |