

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 6
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
Errors are thrown (and should be thrown by you) when a non-happy path is encountered in your code. For example:
Like Python, when an error is thrown, JavaScript yeets the program out of its current context and into the nearest try/catch block, or, if there isn't one, it crashes the program. For example:
const titan = {};
console.log(titan.neck.thickness);
console.log("done");
The code above prints:
TypeError: Cannot read properties of undefined (reading 'thickness')
It never gets to the console.log("done") because the error crashes the program. But what if we use a try/catch block? We place the potentially error-throwing code in the try block, and if an error is thrown, execution immediately jumps to the catch block (ignoring any code in the try block that hasn't run yet):
try {
const titan = {};
console.log(titan.neck.thickness);
console.log("what's a titan?");
} catch (err) {
console.log(err.message);
}
console.log("done");
Which prints:
Cannot read properties of undefined (reading 'thickness')
done
One of the calls to getMessageRecord is throwing a text id not found error. Change the code to safely make the function calls within a try-catch block. If an error is raised, print the error message to the console as a string.