[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/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

20157

try...catch try catch finally try catch finally

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

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

catchStatements

try

exceptionVar

catch catch exceptionVar

finallyStatements

try...catch...finally

try try catch finally catch finally try

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

if fortrycatch finally

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

catch

try exceptionVar catch (e) e catch

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

catch catch catch catch

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

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...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

js
try {
  throw ""; // 
} catch (e) {
  // 
  logMyErrors(e); // 
}

catch e catch

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

js
try {
  myroutine(); // 
} 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("");
  } finally {
    console.log("finally");
  }
} catch (ex) {
  console.error("", ex.message);
}

// 
// "finally"
// "" ""

try catch

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

// 
// "" ""
// ""

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

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

try return finally finally

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

finally

finally return try order.sort() finally finally finally return return

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

js
function doIt() {
  try {
    throw "catch";
  } finally {
    return "finally";
  }
}
doIt(); // finally

finally

ECMAScript 2027 LanguageSpecification
# sec-try-statement


Web Proxy Viewer  |  New URL  |  Original Page