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:
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.
| Error Type | When detected | Example |
|---|---|---|
| Syntax error | Compile time (before running) | Missing ENDIF, misspelled keyword |
| Runtime error (exception) | During execution | Division by zero, file not found |
| Logic error | During/after execution (wrong output) | Wrong formula, off-by-one loop |
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.
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
8 questions · instantly marked · AQA 7517 standard
| Term | Definition |
|---|
10 questions · 10 minutes