SLIDE 1 / 10
CSZone.co.uk
Click anywhere to advance · Arrow keys also work
AQA 7517 · Paper 1 · 4.1.1g

Exception
Handling

AQA A-Level Computer Science · Section 4.1 Fundamentals of Programming

WHAT YOU'LL LEARN
What exceptions are · try/except · Types of exceptions · Why handle errors
AQA SPEC LINK
4.1.1 — Exception handling in programming
What is an Exception?

Runtime Errors & Exceptions

An exception is an error that occurs at runtime (while the program is running) — even if the code is syntactically correct. If unhandled, it crashes the program.
ValueError — e.g. int("abc") — cannot convert non-numeric string
ZeroDivisionError — e.g. 10 / 0
IndexError — accessing element beyond array bounds
FileNotFoundError — trying to open a file that doesn't exist
try / except

Handling Exceptions in Python

BASIC STRUCTURE
try:
  # Code that might raise an exception
  value = int(input("Enter a number: "))
  result = 100 / value
  print(f"Result: {result}")
except ValueError:
  print("Error: Please enter a valid integer")
except ZeroDivisionError:
  print("Error: Cannot divide by zero")
AQA Pseudocode

Exception Handling in AQA Pseudocode

AQA FORMAT
TRY
  x ← INT(USERINPUT)
  result ← 100 DIV x
  OUTPUT result
EXCEPT
  OUTPUT "An error occurred - check your input"
ENDTRY
AQA pseudocode uses a simple TRY...EXCEPT...ENDTRY structure without specifying the exception type. In Python you can be more specific.
else & finally

Extended Exception Handling

try:
  n = int(input("Enter number: "))
  result = 100 / n
except ValueError:
  print("Not a valid number")
except ZeroDivisionError:
  print("Cannot divide by zero")
else:
  print(f"Success: {result}")
finally:
  print("Program complete")
else: runs if no exception occurred · finally: always runs regardless — use for cleanup (e.g. closing files)
Why Handle Exceptions?

Benefits of Exception Handling

Prevents crashes — program continues gracefully instead of terminating abruptly
User-friendly messages — meaningful error output instead of technical tracebacks
Robustness — programs can recover from unexpected input or conditions
Validation — catch invalid input types without crashing (e.g. letters when numbers expected)
Logging — catch errors silently and log them for developers to review later
Input Validation

Exception Handling for Validation

VALIDATION LOOP WITH EXCEPTION HANDLING
valid = False
while not valid:
  try:
    age = int(input("Enter your age: "))
    if 0 <= age <= 120:
      valid = True
    else:
      print("Age must be 0-120")
  except ValueError:
    print("Please enter a whole number")
Exception Types

Common Python Exceptions (AQA)

ExceptionCauseExample
ValueErrorWrong value type/formatint("abc")
ZeroDivisionErrorDivision by zero5 / 0
IndexErrorIndex out of boundslist[10]
TypeErrorWrong data type operation"5" + 3
FileNotFoundErrorFile doesn't existopen("x.txt")
KeyErrorDictionary key not foundd["missing"]
AQA Exam Style

Practice Question

AQA 7517 — Paper 1 Style
A program asks the user to enter two numbers and outputs the result of dividing the first by the second.

Explain why exception handling should be used in this program and describe TWO exceptions that could occur.
[4 marks]
1 mark
Exception handling prevents the program from crashing if an error occurs at runtime
1 mark
ValueError — user enters a non-numeric string (e.g. "abc") which cannot be converted to int/real
1 mark
ZeroDivisionError — user enters 0 as the second number, causing division by zero
1 mark
Without handling, the program would crash and display an unhelpful error message to the user
Summary

Key Points to Remember

An exception is a runtime error that stops execution if unhandled
AQA uses TRY...EXCEPT...ENDTRY; Python uses try/except
Common exceptions: ValueError, ZeroDivisionError, IndexError, TypeError
finally: always runs — use for closing files or cleanup operations
Exception handling makes programs robust and user-friendly
🎉 Lesson complete — move to the quiz!