B2.1b · Book pp. 333–341
Programming fundamentals, part 2
All programmers write bugs. The skill lies in handling what can go wrong, and finding faults calmly.
What you need to know
- B2.1.3 Describe how programs use common exception handling techniques.
- B2.1.4 Construct and use common debugging techniques.
| Error | Meaning | When | Example |
|---|---|---|---|
| Syntax error | Breaks the language’s rules | Found before the code runs | A missing colon |
| Runtime error | A problem during execution | May stop the program | Division by 0, a missing file |
| Logic error | Runs, but gives a wrong result | Found only by testing | Dividing by 2 for an average of three |
B2.1.3 Exception handling
An exception is an event that interrupts a program, such as bad input or a missing file. Exception handling deals with it, so the program can carry on and the user learns what went wrong.
- try: code that might fail.
- except (catch in Java): what to do if it fails.
- finally: code that always runs afterwards.
Try inputs of 5, 0 and hello.
✏️ Book note
The book’s example calls int(input()) outside the try block, so typing “hello” still crashes it. Put the conversion inside the try, as here. A named exception is also better than a bare except:, which hides every kind of bug.
B2.1.4 Debugging
| Trace table | Predict variables, conditions and output on paper |
| Breakpoint | Pause execution at a chosen line |
| Step-by-step | Run one line at a time and watch variables |
| Print statements | Show values and progress, then remove them when done |
✏️ Book correction
The book says Step over skips a line and runs the next. It does not skip. Step over runs the current line in full, including any function it calls, then pauses on the next line. Step into goes inside the called function.
Fix the bugs
Show the two bugs
total = m should be total = total + m, and the divisor should be 3, or better, len(marks). Both are logic errors, so no error message appears.
What does this algorithm do?
Trace it by hand for 1221, then for 1231, and run it to check.
Answer
It checks whether a number is a palindrome, by reversing its digits into temp and comparing with the original.
Quick quiz
Exam-style questions
These are written for this site in the style of Paper 2. Answer on paper first, then open the mark points.
-
Define exception and exception handling. [2]
Mark points
- An exception is an unexpected event that interrupts a program, such as division by 0 [1].
- Exception handling is responding to it, so the program does not stop unexpectedly [1].
-
Identify three possible causes of an exception. [3]
Mark points
- Division by zero [1].
- A file not found [1].
- Invalid input, such as text instead of a number [1].
-
Construct code that asks for a number and prints 10 divided by it, handling invalid input and zero. [4]
Mark points
- Place the input and division in a try block [1].
- Print the result [1].
- except ValueError with a suitable message [1].
- except ZeroDivisionError with a suitable message [1].
-
Outline three IDE features that support debugging. [3]
Mark points
- Breakpoints pause execution at a line [1].
- Stepping runs code one line at a time [1].
- A variables window shows the current values [1].
-
Describe how print statements help debugging, with one drawback. [3]
Mark points
- They show whether a line was reached [1].
- They show variable values at that point [1].
- They must be removed afterwards [1].