| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Statements/try...catch | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
try...catch try, , , catch.
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:
try {...} catch {...}try {...} finally {...}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, try. , , catch.
try {
throw "myException"; //
} catch (e) {
//
logMyErrors(e); //
}
catch (e ) ( , throw). catch.
catch" catch" , try...catch if...else if...else, :
try {
myroutine(); //
} 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; //
}
}
try , exception_var (. . e catch (e)) . , . catch.
try {
if (!firstValidation()) {
throw 1;
}
if (!secondValidation()) {
throw 2;
}
} catch (e) {
// 1 2 ( )
console.log(e);
}
finally try catch. , finally , . finally , catch . finally , . , .
, , , , . :
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:
try {
window.addEventListener("scroll", expensiveCalculations);
maybeThrowError();
} catch {
maybeThrowError();
} finally {
window.removeEventListener("scroll", expensiveCalculations);
}
: . , - ( JavaScript ). . , . finally:
openMyFile();
try {
// -
writeMyFile(theData);
} finally {
closeMyFile(); // ,
}
:
try {
try {
throw new Error("");
} finally {
console.log("finally");
}
} catch (e) {
console.error(" catch", e.message);
}
// :
// "finally"
// " catch" ""
try, catch:
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"
,
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 - , , try...catch...finally, return try catch. , 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);
}
})();
// Logs:
// "inner" "oops"
// "finally"
"" - return finally. , catch.
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-try-statement |
This page was last modified on 24 . 2025 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |