Skip to main content

JavaScript Error Handling Patterns — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Error Handling Patterns lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Return a fallback

    easy10 min

    Parse the text and return null when it fails.

  2. 2

    Fail fast on bad input

    easy10 min

    Check the types at the top and throw immediately.

  3. 3

    Use a guard clause

    easy10 min

    Return a default rather than throwing for missing input.

  4. 4

    Add context when rethrowing

    easy10 min

    Replace the parse error with a clearer message.

  5. 5

    Handle at the boundary

    medium10 min

    Let the loop fail and catch once at the top.

Find the bug(5)

  1. 1

    Fix the missing input check

    easy8 min

    With no check the text is coerced and a wrong answer is returned.

  2. 2

    Fix the error caught and ignored

    easy8 min

    Catching without acting hides a genuine bug.

  3. 3

    Fix the rethrow with no new information

    easy8 min

    Rethrowing the same message adds nothing.

  4. 4

    Fix the throw where a default would do

    easy8 min

    A missing name is expected, so return a greeting instead.

  5. 5

    Fix the handler inside the loop

    easy8 min

    One handler at the boundary is enough.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace