Python traps · Chapter 10 of 16

Exceptions swallowed by a bare except or a finally that returns

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Time
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: charged, and no tiffin

    Write reproduce() in repro.py. Make a fake db, an object whose save(order) raises ConnectionError("db timeout"), and a fake log, an object with info(line) and error(line) methods that do nothing or remember their lines. Call place_order(order, db, log) from checkout.py with the order {"id": "D2047", "customer": "C025"} and your fakes. If it returns, return what it returned; if it raises, catch the error and return its message, str(error).

    The check runs your reproduce() twice: beside the original checkout.py it must return 'D2047' (the order looks placed and the error is gone), and beside a fixed checkout.py it must return 'db timeout'. So the fake db must really raise. No real waiting, no real database and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    checkout.py: a failed save that everyone hears about

    Fix place_order(order, db, log) in checkout.py. When db.save(order) works, it logs log.info("order <id>: saved for <customer>") and returns order["id"], with no error logged. When db.save raises an error (any Exception), it calls log.error once with a line holding the order's id and the error's message (for example order D2047: save failed: db timeout), logs no info line, and lets that same exception reach the caller. When db.save raises KeyboardInterrupt (Ctrl+C), place_order does not catch it.

    The check imports a fresh copy of your checkout.py and uses its own fake db and log and its own errors (not only ConnectionError). Catching Exception and logging it is not enough while a return inside finally is still there: that return throws the error away.

  3. Task 3

    test_checkout.py: a test that would have caught it

    Write a pytest file, test_checkout.py, that would have caught this bug before customers paid for nothing. The check runs your file three times: beside the original checkout.py, where at least one test must fail; beside the fixed checkout.py, where every test must pass; and beside a wrong fix that catches Exception and logs it but keeps the return inside finally, where at least one test must fail.

    So with a fake db whose save raises, check that the error reaches the caller with pytest.raises, not only that it was logged; and check that a good save still returns the order's id. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: charged, and no tiffin
  • checkout.py: a failed save that everyone hears about
  • test_checkout.py: a test that would have caught it

More in Time

Build "Exceptions swallowed by a bare except or a finally that returns" yourself

DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps are free. This one comes with the all labs plan.