A2 · Practice questions · Exception handling

Catch the exception.

Six original Cambridge-style questions on exceptions and exception handling, including tracing and writing handler code, with mark-scheme solutions.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Get the marks

Try, catch, recover.

01
Explain
[3 marks]

Explain what is meant by an exception and give two examples of situations that cause one.

  • An unexpected event / error that occurs while the program is running and disrupts the normal flow of execution. ✓
  • Example 1: trying to open a file that does not exist. ✓
  • Example 2: division by zero / converting non-numeric text to a number / array index out of range / loss of a network connection. ✓
02
Explain
[3 marks]

Explain why exception handling is important in a program that saves customer orders to a file.

  • Without it, a run-time error (e.g. disk full or file locked) would crash the program. ✓
  • The handler can give the user a meaningful message and let them retry or save elsewhere. ✓
  • Data already entered is not lost and the file can be closed safely, so it is not corrupted. ✓
03
Trace
[4 marks]

A Python program processes the list values = ["8", "0", "x", "4"]. For each item it runs, in a try block, n = int(item) followed by print(24 / n). The handlers are except ValueError: print("not a number") and except ZeroDivisionError: print("cannot divide"). State the output.

  • "8": 24 / 8 = 3.0 is output. ✓
  • "0": int succeeds but 24 / 0 raises ZeroDivisionError: cannot divide. ✓
  • "x": int("x") raises ValueError: not a number. ✓
  • "4": 24 / 4 = 6.0 is output. ✓
04
Write
[5 marks]

Write program code in a language of your choice that asks the user for the name of a file, opens it for reading and outputs every line. If the file cannot be found, the program must output a suitable message and ask for another file name until one can be opened.

  • A loop that repeats until a file has been opened successfully (e.g. a Boolean flag). ✓
  • Input of the file name inside the loop. ✓
  • The open statement inside a try block, setting the flag when it succeeds. ✓
  • An except / catch block for the file-not-found exception that outputs a meaningful message. ✓
  • After the loop, every line is read and output and the file is closed. ✓
05
Explain
[2 marks]

State the purpose of a finally block and give an example of a statement that should be placed in one.

  • It contains code that runs whether or not an exception was raised. ✓
  • Example: closing a file / releasing a resource / closing a network connection. ✓
06
Analysis
[4 marks]

A student says: "I will put my whole program in one try block with one handler that catches every exception, so it can never crash." Evaluate this approach.

  • It would stop the program crashing, but after any error the rest of the try block is skipped, so the program cannot carry on with its task. ✓
  • A single general handler cannot give a meaningful message or a suitable recovery for each different error. ✓
  • It can hide logic errors (bugs) that should be found and fixed, making testing harder. ✓
  • Better: put only risky statements in small try blocks with specific handlers, and use validation for predictable errors. ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.