Follow the transformation
Read the exception type and traceback from the bottom upward to identify the failing operation.
Catch only exceptions you can handle meaningfully.
Raise a specific exception when input violates a contract.
Debug with Print and Breakpoints is part of Python's error-handling and verification toolkit.
Debug with Print and Breakpoints is part of Python's error-handling and verification toolkit. Errors are evidence about violated syntax, runtime conditions or assumptions; robust programs make expected failure cases explicit rather than hiding them.
Debug with Print and Breakpoints matters because robust software must distinguish programmer defects from expected bad inputs and must provide evidence that important behaviour still works after changes. Error handling and tests make those boundaries explicit.
Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Read the exception type and traceback from the bottom upward to identify the failing operation. Stage 2: Catch only exceptions you can handle meaningfully. Stage 3: Raise a specific exception when input violates a contract. Final checkpoint: Write tests for normal cases, boundaries and expected failures.
Read the exception type and traceback from the bottom upward to identify the failing operation.
Catch only exceptions you can handle meaningfully.
Raise a specific exception when input violates a contract.
Read the exception type and traceback from the bottom upward to identify the failing operation. This is an input-preparation stage for Debug with Print and Breakpoints. Verify the relevant type, shape, units, keys, missingness or assumptions before later steps depend on them.
# Step 1 — Define the reusable `parse_age` function; its indented body describes what happens for each call.
def parse_age(text):
# Step 2 — Start the operation that may fail so the expected exception can be handled explicitly.
try:
# Step 3 — Compute the right-hand expression and store its result in `age` for the next step.
age = int(text)
# Step 4 — Handle the expected failure path instead of allowing the program to terminate unexpectedly.
except ValueError as exc:
# Step 5 — Raise an explicit exception to signal that the required condition or input contract was violated.
raise ValueError("age must be a whole number") from exc
# Step 6 — Evaluate this condition and execute the indented branch only when the condition is true.
if age < 0:
# Step 7 — Raise an explicit exception to signal that the required condition or input contract was violated.
raise ValueError("age cannot be negative")
# Step 8 — Return the computed value to the caller so the result can be reused or tested.
return age
# Step 9 — Iterate through the collection so the indented block is applied once for each item.
for value in ["34", "abc"]:
# Step 10 — Start the operation that may fail so the expected exception can be handled explicitly.
try:
# Step 11 — Display the current value explicitly so the result/state can be inspected during execution.
print(parse_age(value))
# Step 12 — Handle the expected failure path instead of allowing the program to terminate unexpectedly.
except ValueError as err:
# Step 13 — Display the current value explicitly so the result/state can be inspected during execution.
print(type(err).__name__ + ":", err)34 ValueError: age must be a whole number
For Debug with Print and Breakpoints, connect the displayed result to the specific input and mechanism above; independently verify one value/state change rather than treating successful execution as proof.
Syntax errorProgram cannot be parsed correctly.Exception / runtime errorFailure detected while executing a valid program.AssertionDeveloper check for an invariant that should be true.Unit testRepeatable test of a small unit of behaviour.Use Debug with Print and Breakpoints when the program genuinely needs this language behaviour and you can state the input object, resulting value/state and expected failure behaviour.
Choose a clearer built-in, data structure or control-flow pattern when it expresses the intent more directly; stop if implicit conversion, mutation or hidden state makes the behaviour hard to reason about.
Build a tiny, inspectable example of Debug with Print and Breakpoints. First read the exception type and traceback from the bottom upward to identify the failing operation. Then catch only exceptions you can handle meaningfully. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.
Before trusting a result from Debug with Print and Breakpoints, which check provides the strongest evidence that you understand and applied it correctly?
Step 1Read the exception type and traceback from the bottom upward to identify the failing operation.Step 2Catch only exceptions you can handle meaningfully.Step 3Raise a specific exception when input violates a contract.Step 4Use finally for cleanup that must occur whether the operation succeeds or fails.