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

try...catch try, , , catch.

In this article

try {
   try_statements
}
[catch (exception_var_1 if condition_1) { //  
   catch_statements_1
}]
...
[catch (exception_var_2) {
   catch_statements_2
}]
[finally {
   finally_statements
}]
try_statements

.

catch_statements_1, catch_statements_2

, , try.

exception_var_1, exception_var_2

, catch

condition_1

.

finally_statements

, try. , .

try try, ( ({} ) , ), catch finally. , try:

  1. try {...} catch {...}
  2. try {...} finally {...}
  3. try {...} catch {...} finally {...}

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

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

try. try catch ( try {...} finaly {...}, try {...} catch finally), atch try.

try JavaScript ( , ). JavaScript JavaScript .

catch

catch, try. , , catch.

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

catch (e ) ( , throw). catch.

catch

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

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

try , exception_var (. . e catch (e)) . , . catch.

js
try {
  if (!firstValidation()) {
    throw 1;
  }
  if (!secondValidation()) {
    throw 2;
  }
} catch (e) {
  //  1  2 (     )
  console.log(e);
}

finally

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

, , , , . :

js
function expensiveCalculations() {
  //  
}

function maybeThrowError() {
  // ,    
  if (Math.random() > 0.5) throw new Error();
}

try {
  //      
  //  ,    
  // 
  window.addEventListener("scroll", expensiveCalculations);
  maybeThrowError();
} catch {
  //   maybeThrowError  ,
  //      catch 
  //     
  //  
  maybeThrowError();
}
window.removeEventListener("scroll", expensiveCalculations);

, maybeThrowError try, catch. catch , , , , . , , finally:

js
try {
  window.addEventListener("scroll", expensiveCalculations);
  maybeThrowError();
} catch {
  maybeThrowError();
} finally {
  window.removeEventListener("scroll", expensiveCalculations);
}

: . , - ( JavaScript ). . , . finally:

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

try

:

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

// :
// "finally"
// "  catch" ""

try, catch:

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

// Output:
// "  catch" ""
// "finally"

,

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

// :
// "  catch" "oops"
// "finally"
// "  catch" "oops"

catch, . , ( catch ), .

finally

finally - , , try...catch...finally, return try catch. , 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"

"" - return finally. , catch.

Specification
ECMAScript 2027 LanguageSpecification
# sec-try-statement


Web Proxy Viewer  |  New URL  |  Original Page