Six original Cambridge-style questions on exceptions and exception handling, including tracing and writing handler code, with mark-scheme solutions.
Explain what is meant by an exception and give two examples of situations that cause one.
Explain why exception handling is important in a program that saves customer orders to a file.
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.
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.
State the purpose of a finally block and give an example of a statement that should be placed in one.
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.
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.