Recall & Review
beginner
What is the purpose of
try...catch in JavaScript?It is used to handle runtime errors gracefully by trying a block of code and catching errors if they occur, preventing the program from crashing.
Click to reveal answer
beginner
What does the
catch block receive as its parameter?The
catch block receives the error object that was thrown in the try block, which contains information about the error.Click to reveal answer
intermediate
How can you throw a custom error in JavaScript?
You can use the
throw statement followed by an error object or message, for example: throw new Error('Custom message').Click to reveal answer
beginner
What happens if an error occurs outside a
try...catch block?The error will be unhandled and usually causes the program to stop or crash, showing an error message in the console.
Click to reveal answer
intermediate
Can you use
try...catch to handle asynchronous errors directly?No,
try...catch works for synchronous code. For asynchronous code, you handle errors with catch on promises or use async/await with try...catch.Click to reveal answer
What keyword starts a block of code where you expect an error might happen?
✗ Incorrect
The
try block contains code that might throw an error.Which block runs only if an error occurs in the
try block?✗ Incorrect
The
catch block runs when an error is thrown inside the try block.How do you create a new error to throw?
✗ Incorrect
You create an error object with
new Error('message') and then throw it.What happens if you don't catch an error?
✗ Incorrect
Uncaught errors usually cause the program to stop or crash.
Which is the correct way to catch errors from a promise?
✗ Incorrect
Promises use the
catch method to handle errors asynchronously.Explain how
try...catch helps in handling runtime errors in JavaScript.Think about what happens when code inside try fails.
You got /4 concepts.
Describe how you would handle errors in asynchronous JavaScript code.
Remember that async code needs special handling.
You got /4 concepts.