Skip to the content

finally and throw

JavaScript · Unit 9: Errors and exceptions

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. 1. Predict the output

    What does this code print?

  2. 2. Multiple choice

    When does the finally block run?

  3. 3. Predict the output

    What does this code print?

  4. 4. Predict the output

    What does this code print?

  5. 5. Complete the code

    Complete it so a negative age is an error that the catch catches.

  6. 6. Find the bug

    signUp should accept only adults (18 or older). With 20 it should print "Signed up". Which line has the error?

  7. 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. 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. 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. 10. Predict the output

    You put it all together in a loop. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →