← IB Computer Science

B2.1b · Book pp. 333–341

Programming fundamentals, part 2

Your level

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.
ErrorMeaningWhenExample
Syntax errorBreaks the language’s rulesFound before the code runsA missing colon
Runtime errorA problem during executionMay stop the programDivision by 0, a missing file
Logic errorRuns, but gives a wrong resultFound only by testingDividing by 2 for an average of three

Next: exceptions →

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.

Next: debugging →

B2.1.4 Debugging

Trace tablePredict variables, conditions and output on paper
BreakpointPause execution at a chosen line
Step-by-stepRun one line at a time and watch variables
Print statementsShow values and progress, then remove them when done

🔬 Lab: a debugger

Click a line to set a breakpoint, then Run. The program counts how many scores pass.

score
count
Output

✏️ 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.

Next: review →

Quick quiz

1. A program gives an average that is too high, without crashing. This is a…
2. A missing file while running is a…
3. In Python, which block handles an exception?
4. When does finally run?
5. What does a breakpoint do?
6. Step over on a function call…
7. Trace tables are mainly used to…
8. Why avoid showing users full error details?

Exam-style questions

These are written for this site in the style of Paper 2. Answer on paper first, then open the mark points.

  1. 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].
  2. 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].
  3. 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].
  4. 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].
  5. 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].