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

try...catch - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

try...catch

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

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

In this article

try {
  nonExistentFunction();
} catch (error) {
  console.error(error);
  // Expected output: ReferenceError: nonExistentFunction is not defined
  // (Note: the exact output may be browser-dependent)
}

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

try .

catchStatements

try .

exceptionVar Optional

catch . catch exceptionVar .

finallyStatements

try...catch...finally . .

try try . catch finally . catch finally . try .

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

if for , try, catch, finally {} .

js
try doSomething(); // SyntaxError
catch (e) console.log(e);

catch try . try ( try ) , catch . try catch .

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

try . try catch , try catch . , , try , catch .

, try JavaScript . JavaScript JavaScript .

Catch

try , exceptionVar(, catch (e) e) . binding . binding catch scope .

exceptionVar . .

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

catch catch . catch catch . ( , .)

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 "Oops; this is not an Error object";
} 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
  • try catch (return, throw, break, continue)

catch try finally . finally .

finally . . finally .

js
openMyFile();
try {
  // tie up a resource
  writeMyFile(theData);
} finally {
  closeMyFile(); // always close the resource
}

finally (return, throw, break, continue) try catch "". , try 1 , finally finally (2) .

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

doIt(); // returns 2

finally . finally .

catch

catch try catch . , catch .

js
try {
  throw "myException"; // generates an exception
} catch (e) {
  // statements to handle any exceptions
  logMyErrors(e); // pass exception object to error handler
}

catch (e ) . catch .

catch

try...catch if...else if...else " catch " . , .

js
try {
  myroutine(); // may throw three types of exceptions
} catch (e) {
  if (e instanceof TypeError) {
    // statements to handle TypeError exceptions
  } else if (e instanceof RangeError) {
    // statements to handle RangeError exceptions
  } else if (e instanceof EvalError) {
    // statements to handle EvalError exceptions
  } else {
    // statements to handle any unspecified exceptions
    logMyErrors(e); // pass exception object to error handler
  }
}

catch , .

js
try {
  myRoutine();
} catch (e) {
  if (e instanceof RangeError) {
    // statements to handle this very common expected error
  } else {
    throw e; // re-throw the error unchanged
  }
}

Java .

java
try {
  myRoutine();
} catch (RangeError e) {
  // statements to handle this very common expected error
}
// Other errors are implicitly re-thrown

try

, .

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

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

catch try :

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

// Logs:
// "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);
}

// Logs:
// "inner" "oops"
// "finally"
// "outer" "oops"

catch , catch . "" (, catch ) , "" .

finally

finally , try-catch-finally . try catch return . 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);
  }
})();

// Logs:
// "inner" "oops"
// "finally"

finally ( try-catch-finally ) "oops" . catch .

Specification
ECMAScript 2027 LanguageSpecification
# sec-try-statement


Web Proxy Viewer  |  New URL  |  Original Page