Recall & Review
beginner Click to reveal answer
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.
beginner Click to reveal answer
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.intermediate Click to reveal answer
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').beginner Click to reveal answer
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.
intermediate Click to reveal answer
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.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.
