finally and throw
JavaScript · Unit 9: Errors and exceptions
- Full plan
- JavaScript
- 10 exercises
finally goes after the catch and always runs, error or not. It's for whatever has to happen anyway, like saying you're done.
try {
console.log("Charging...");
console.log(totl);
} catch (error) {
console.log("Payment failed");
} finally {
console.log("Thank you");
}Prints
Charging... Payment failed Thank you
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Multiple choice
When does the
finallyblock run?3. Predict the output
What does this code print?
4. Predict the output
What does this code print?
5. Complete the code
Complete it so a negative age is an error that the
catchcatches.6. Find the bug
signUpshould accept only adults (18 or older). With 20 it should print "Signed up". Which line has the error?7. Multiple choice
Your function gets how many items someone buys, and 0 isn't allowed. Which error helps whoever reads it the most?
8. Put the lines in order
Order it so a grade over 10 is an error and only its message gets printed, "Max 10".
9. Complete the code
Complete it so signing up someone who's already in is an error. It should print "Duplicate: Ava" and then 2.
10. Predict the output
You put it all together in a loop. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.