

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Error Object
incomplete
2: Handling Errors
incomplete
3: Finally
incomplete
4: Throwing Errors
incomplete
5: When to try/catch
incomplete
This lesson's interactive features are locked, please to keep using them
We missed a block. While a try/catch is the most common block you'll see, there is also a finally block.
The code in the
finallyblock will always be executed before control flow exits the entire construct.
It's for if you want something to run regardless of what nonsense happens in the try and catch blocks. In some crazy scenarios (try to avoid this), you might have an error thrown in the catch block. But even if that happens, the finally block will still run. In this example:
try {
const titan = {};
console.log(titan.neck.thickness);
console.log("what's a titan?");
} catch (err) {
console.log(err.message);
} finally {
console.log("This will always run regardless of any errors.");
}
This is what gets printed:
Cannot read properties of undefined (reading 'thickness')
This will always run regardless of any errors.
Try running the code in its current state. Notice how Cleanup complete does not get logged. Fix the code so that Cleanup complete is logged regardless of any errors that may occur.