[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/try...catch [Back]  [Original]

try...catch - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

try...catch

Baseline

20157

try...catch try catch finally try catch finally

try {
  nonExistentFunction();
} catch (error) {
  console.error(error);
  // : ReferenceError: nonExistentFunction is not defined
  // 
}

js
try {
  tryStatements
} catch (exceptionVar) {
  catchStatements
} finally {
  finallyStatements
}
tryStatements

catchStatements

try

exceptionVar

catch catch exceptionVar catch {...}

finallyStatements

try...catch...finally

try try catch finally catch finally try 3

  • try...catch
  • try...finally
  • try...catch...finally

if for trycatchfinally

js
try 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

catch

try exceptionVar catch (e) e catch

js
try {
  throw new TypeError("oops");
} catch ({ name, message }) {
  console.log(name); // "TypeError"
  console.log(message); // "oops"
}

catch catch catch catch 1

js
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
}

Error

js
try {
  throw " Error ";
} catch (e) {
  if (!(e instanceof Error)) {
    e = new Error(e);
  }
  console.error(e.message);
}

js
function isValidJSON(text) {
  try {
    JSON.parse(text);
    return true;
  } catch {
    return false;
  }
}

finally

finally try catch try...catch...finally finally

  • try
  • catch
  • returnthrowbreakcontinue try catch

finally finally catch

finally finally

js
openMyFile();
try {
  // 
  writeMyFile(theData);
} finally {
  closeMyFile(); // 
}

return, throw, break, continue finally try catch try 1 finally finally

js
function doIt() {
  try {
    return 1;
  } finally {
    return 2;
  }
}

doIt(); // returns 2

finally

catch

catch try catch catch

js
try {
  throw new Error("My exception"); // 
} catch (e) {
  // 
  logMyErrors(e); // 
}

catch e catch

catch

catch try...catch if...else if...else

js
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); // 
  }
}

js
try {
  myRoutine();
} catch (e) {
  if (e instanceof RangeError) {
    // 
  } else {
    throw e; // 
  }
}

Java

java
try {
  myRoutine();
} catch (RangeError e) {
  // 
}
// 

try

js
try {
  try {
    throw new Error("oops");
  } finally {
    console.log("finally");
  }
} catch (ex) {
  console.error("outer", ex.message);
}

// :
// "finally"
// "outer" "oops"

try catch

js
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"

js
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 return

finally try catch return try-catch-finally catch

js
(() => {
  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