Recall & Review
beginner
What is the purpose of the
finally block in JavaScript?The
finally block contains code that always runs after the try and catch blocks, no matter if an error occurred or not.Click to reveal answer
beginner
When does the code inside a
finally block execute?It executes after the
try block finishes, whether an error was thrown or not, and after the catch block if an error was caught.Click to reveal answer
intermediate
Can the
finally block change the return value of a function?Yes, if the
finally block contains a return statement, it overrides any previous return from try or catch.Click to reveal answer
advanced
What happens if an error is thrown inside the
finally block?The error thrown in
finally will replace any previous error or return value, and it will propagate out of the whole try-catch-finally structure.Click to reveal answer
beginner
Write a simple example of a
try-catch-finally block in JavaScript.try {
console.log('Try block runs');
throw new Error('Oops');
} catch (e) {
console.log('Catch block runs:', e.message);
} finally {
console.log('Finally block always runs');
}
Click to reveal answer
When does the
finally block run in a try-catch-finally statement?✗ Incorrect
The
finally block always runs after the try and catch blocks, regardless of errors.What happens if there is a return statement inside both
try and finally blocks?✗ Incorrect
The
finally block's return overrides any previous return from try or catch.If an error is thrown inside the
finally block, what happens?✗ Incorrect
An error thrown in
finally replaces any previous error or return value and propagates out.Which of these is a correct use of
finally?✗ Incorrect
finally is often used to clean up resources regardless of errors.Can a
finally block exist without a catch block?✗ Incorrect
A
finally block can follow a try block without a catch block.Explain the role of the
finally block in JavaScript error handling and when it executes.Think about what code you want to run no matter what happens in try or catch.
You got /3 concepts.
Describe what happens if a return statement is present in both the
try and finally blocks.Consider which return value the function actually sends back.
You got /3 concepts.