| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/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);
// : ReferenceError: nonExistentFunction is not defined
//
}
try {
tryStatements
} catch (exceptionVar) {
catchStatements
} finally {
finallyStatements
}
tryStatementscatchStatementstry
exceptionVar catch catch exceptionVar catch {...}
finallyStatementstry...catch...finally
try try catch finally catch finally try 3
try...catchtry...finallytry...catch...finallytry doSomething(); // SyntaxError
catch (e) console.log(e);
catch try try try catch try catch
finally try...catch...finally
1 try try catch try catch
try JavaScript JavaScript JavaScript
try exceptionVar catch (e) e catch
try {
throw new TypeError("oops");
} catch ({ name, message }) {
console.log(name); // "TypeError"
console.log(message); // "oops"
}
catch catch catch catch 1
try {
throw new TypeError("oops");
} 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 catch returnthrowbreakcontinue try catch finally finally catch
finally finally
openMyFile();
try {
//
writeMyFile(theData);
} finally {
closeMyFile(); //
}
return, throw, break, continue finally try catch try 1 finally finally
function doIt() {
try {
return 1;
} finally {
return 2;
}
}
doIt(); // returns 2
finally
catch try catch catch
try {
throw new Error("My exception"); //
} catch (e) {
//
logMyErrors(e); //
}
catch try...catch if...else if...else
try {
myroutine(); // 3
} 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("oops");
} finally {
console.log("finally");
}
} catch (ex) {
console.error("outer", ex.message);
}
// :
// "finally"
// "outer" "oops"
try 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);
}
// :
// "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);
}
// :
// "inner" "oops"
// "finally"
// "outer" "oops"
catch catch
finally try catch return try-catch-finally
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);
}
})();
// :
// "inner" "oops"
// "finally"
"oops" finally return catch
| ECMAScript 2027 LanguageSpecification # sec-try-statement |
| Web Proxy Viewer | New URL | Original Page |