Try…catch
many times for many reason programmers have some mistakes. No matter how great they are at programming. A script “dies” (immediately stops) in case of an error. But there’s a syntax construct try...catch
that allows us to “catch” errors so the script can, instead of dying, do something more reasonable.
The try...catch
construct has two main blocks: try
, and then catch
:
try {
// code…
} catch (err) {
// error handling
}
try...catch
only works for runtime errors: For try...catch
to work, the code must be runnable. In other words, it should be valid JavaScript.
It won’t work if the code is syntactically wrong, for instance it has unmatched curly braces:
try { {{{{{{{{{{{{
} catch (err) {
alert(“The engine can’t understand this code, it’s invalid”); }
The JavaScript engine first reads the code, and then runs it. The errors that occur on the reading phase are called “parse-time” errors and are unrecoverable (from inside that code). That’s because the engine can’t understand the code.
So, try...catch
can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”.
Block Bindings
ECMAScript 6 offers options to make controlling scope easier. This chapter demonstrates why classic var declarations can be confusing, introduces block-level bindings in ECMAScript 6, and then offers some best practices for using them.
Var Declarations and Hoisting: Variable declarations using var are treated as if they are at the top of the function (or global scope, if declared outside of a function) regardless of where the actual declaration occurs; this is called hoisting. For a demonstration of what hoisting does, consider the following function definition:
function getValue(condition) {
if (condition) {
var value = "blue";
// other code
return value;
} else {
// value exists here with a value of undefined
return null;
}
// value exists here with a value of undefined
}
Block-Level Declarations: Block-level declarations are those that declare variables that are inaccessible outside of a given block scope. Block scopes are created:
- Inside of a function
- Inside of a block (indicated by the
{
and}
characters)
Block scoping is how many C-based languages work, and the introduction of block-level declarations in ECMAScript 6 is intended to bring that same flexibility (and uniformity) to JavaScript.
Let Declarations
The let declaration syntax is the same as the syntax for var . You can basically replace var with let to declare a variable, but limit the variable's scope to only the current code block (there are a few other subtle differences discussed a bit later, as well). Since let declarations are not hoisted to the top of the enclosing block, you may want to always place let declarations first in the block, so that they are available to the entire block. Here's an example:
function getValue(condition) { if (condition) {
let value = "blue"; // other code return value;
} else { // value doesn't exist here return null;
} // value doesn't exist here
}