📄 Paper 1 · 4.1 Fundamentals of Programming
⭐ Pro
4.1.1e Exception Handling
AQA 7517 · A-Level Computer Science · ~12 min read

What is an Exception?

An exception is an error condition that arises during program execution — an event that disrupts normal program flow. Unlike syntax errors (detected at compile time), exceptions are runtime errors.

Examples of exceptions:

  • Division by zero — attempting to divide a number by 0
  • Invalid input type — user enters a letter when a number is expected
  • File not found — attempting to open a file that doesn't exist
  • Index out of range — accessing array index beyond its bounds
  • Stack overflow — typically from infinite recursion

Exception Handling in AQA Pseudocode

AQA 7517 uses a TRY … EXCEPT … ENDTRY construct for exception handling:

TRY
    // Code that might raise an exception
    INPUT num
    result ← 100 / num
    OUTPUT result
EXCEPT
    // Code to handle the exception
    OUTPUT "Error: division by zero or invalid input"
ENDTRY

The TRY block contains code that might raise an exception. If an exception occurs, execution immediately jumps to the EXCEPT block, which handles it gracefully. Code after the exception in the TRY block is skipped.

Why Use Exception Handling?

  • Prevents program crash — without handling, an exception terminates the program abruptly
  • Provides meaningful feedback — a friendly error message rather than a cryptic runtime error
  • Allows recovery — the program can continue or retry after handling an exception
  • Separation of concerns — error-handling logic is separate from normal logic

Difference Between Error Types

Error TypeWhen detectedExample
Syntax errorCompile time (before running)Missing ENDIF, misspelled keyword
Runtime error (exception)During executionDivision by zero, file not found
Logic errorDuring/after execution (wrong output)Wrong formula, off-by-one loop

Exception Handling in Python (for context)

AQA also expects awareness of how exceptions work in Python:

try:
    num = int(input("Enter a number: "))
    result = 100 / num
    print(result)
except ZeroDivisionError:
    print("Cannot divide by zero")
except ValueError:
    print("Please enter a valid number")

Python allows catching specific exception types — each except clause handles a different type. AQA pseudocode uses a single EXCEPT block.

FINALLY Block (Python)

Python also has a finally block that always executes regardless of whether an exception occurred — useful for cleanup (closing files, releasing resources):

try:
    file = open("data.txt")
finally:
    file.close()  # Always closes the file
Exam tip: Know the AQA pseudocode structure: TRY … EXCEPT … ENDTRY. Be able to explain WHY exception handling is used (prevents crashes, provides feedback). Know the difference between syntax errors (compile-time) and runtime exceptions. In Python context questions, know ZeroDivisionError and ValueError as common exception types.
⚠️ Common Mistakes
  • Calling runtime errors "syntax errors" — syntax errors are detected before running; exceptions are runtime
  • Thinking the TRY block always runs fully — once an exception occurs, the rest of the TRY block is skipped
  • Not knowing the AQA pseudocode keyword — it's EXCEPT (not CATCH as in Java/C#)
  • Confusing logic errors with exceptions — a wrong answer isn't an exception, just incorrect program logic
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.1.1e Exception Handling

8 questions · instantly marked · AQA 7517 standard

Q1Define the term "exception" in the context of programming.[2]
✅ Mark scheme
Mark scheme
An exception is an error condition (event) that occurs during program execution (at runtime) [1] that disrupts the normal flow of the program [1].
Q2State three examples of events that could cause a runtime exception.[3]
✅ Mark scheme
Mark scheme
Any three: division by zero [1]; invalid input type (e.g. entering text when a number expected) [1]; accessing an array index out of bounds [1]; file not found [1]; stack overflow from recursion [1].
Q3Write AQA pseudocode using TRY…EXCEPT to safely handle a potential division by zero when dividing 100 by a user-input value.[4]
✅ Mark scheme
Mark scheme
TRY [1]; INPUT divisor; result ← 100 / divisor; OUTPUT result [1]; EXCEPT [1]; OUTPUT appropriate error message [1]; ENDTRY [1] — penalise missing ENDTRY once; award [4] max for correct structure with minor errors.
Q4Explain what happens to the remaining statements in a TRY block when an exception is raised.[2]
✅ Mark scheme
Mark scheme
Execution immediately jumps to the EXCEPT block [1]; the remaining statements in the TRY block are skipped/not executed [1].
Q5State the difference between a syntax error and a runtime exception, giving one example of each.[4]
✅ Mark scheme
Mark scheme
A syntax error is detected at compile time, before the program runs [1]; e.g. missing ENDIF, misspelled keyword [1]. A runtime exception occurs during program execution [1]; e.g. division by zero, file not found [1].
Q6State two reasons why exception handling is important in a well-designed program.[2]
✅ Mark scheme
Mark scheme
Any two: prevents abrupt program termination/crash [1]; provides a meaningful/friendly error message to the user [1]; allows the program to continue or recover [1]; separates error-handling logic from normal program logic [1].
Q7In Python, what is the purpose of the 'finally' block in exception handling?[2]
✅ Mark scheme
Mark scheme
The finally block always executes [1] regardless of whether an exception was raised or not — used for cleanup tasks such as closing files or releasing resources [1].
Q8A student says "if I write my code correctly there is no need for exception handling." Evaluate this statement.[3]
✅ Mark scheme
Mark scheme
The student is incorrect [1]; some exceptions are caused by unpredictable user input or environmental factors (file not found, network failure) that cannot be prevented by correct code [1]; exception handling allows programs to deal with these situations gracefully rather than crashing [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 10
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Exception Handling

10 questions · 10 minutes

← 4.1.1d String Handling
5 of 70 · AQA 7517
4.1.1f Subroutines →