Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 2 · CPython Internals & Performancecontextlib.contextmanagerpredict21 / 161
+75 XP
Task
📝 **Question:** Predict the exact output. \`track\` is a \`@contextmanager\` generator that prints \`enter:\` / \`exit:\` and has an \`except ValueError\` arm. The first \`with\` block is a clean run. The second raises \`ValueError("oops")\` inside the body — does the program crash before \`print("done")\`, or does the \`with\` swallow it? 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.
Predict the output

Read the code carefully

from contextlib import contextmanager


@contextmanager
def track(label):
    print(f"enter:{label}")
    try:
        yield label.upper()
    except ValueError as e:
        print(f"caught:{e}")
    finally:
        print(f"exit:{label}")


with track("a") as t:
    print(f"body:{t}")

with track("b") as t:
    print(f"body:{t}")
    raise ValueError("oops")

print("done")

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…