Debugging a py file#
Task 2.1 Set breakpoints and start debugging#
Open
debug_example.pyand click left of a line number to add a red breakpoint (e.g., ontotal = 0).Start debugging: press F5 or click the green ‘Run and Debug’ button while
debug_example.pyis active.When execution pauses:
Inspect variables in the ‘Variables’ view.
Use the ‘Debug Console’ to evaluate expressions (e.g.,
v).Use the ‘Call Stack’ to see how you got here.
Task 2.2 Step through code#
Click ‘Step Over’ (F10) multiple times: This runs the current line; if it calls a function, executes it without entering and stops at the next line in the same function. Observe how
vchanges and where the exception occurs.Test the other stepping commands:
‘Continue’ (F5), runs until the next breakpoint, exception or program end.
‘Step Into’ (F11): Enters the called function and pauses at its first line so you can debug inside it.
‘Step Out’ (Shift+F11)
Task 2.3 Break on exceptions and conditional breakpoints#
In the ‘Breakpoints’ view on the bottom left corner, enable breaking on exceptions (‘Raised Exceptions’ / ‘Uncaught Exceptions’).
Right-click the breakpoint (in the gutter or in the Breakpoints view) → ‘Add conditional breakpoint’ and enter a condition to check the division by zero:
If the breakpoint is inside
compute_ratio(), useb == 0(local parameter in that function).If you prefer to stop at the call site, place a breakpoint on
total += compute_ratio(10, v)and set conditionv == 0, then use F11 (Step Into). Note: If there is already a breakpoint, right-click and use ‘Edit Breakpoint…’.
Press F5. Execution pauses when
bis 0. With exception breakpoints enabled, it also pauses onZeroDivisionErroreven without a conditional.
Task 2.4 Adapt#
Adapt the code to handle division by zero gracefully, e.g., return 0 instead of raising an exception. While debugging, you can inspect the Variables, which allows you to confirm confirm
a == 10,b == 0at some point. In the Debug Console (in the bottom toolbar) you can debug expressions safely: trya / (b or 1)to probe safely.Whenever you’re happy with the code, stop debugging (Shift+F5), implement your changes and run the script normally (F5 or Run → Run Without Debugging) to see the output.