SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
CAIE 9618 · Paper 4 · Topic 4.2.3

Exception
Handling

TRY · EXCEPT · ENDTRY · Raising Exceptions · Custom Exception Classes · Graceful Degradation

CSZone Cambridge International AS & A Level Computer Science 9618
What is an Exception?

Runtime Errors vs Exceptions

An exception is an event that disrupts the normal flow of program execution at runtime — something unexpected that the program must handle gracefully rather than crashing. Exceptions differ from syntax errors (caught at compile time) and logic errors (wrong result but no crash).
COMMON EXCEPTIONS IN CAIE 9618
Division by zero — divide by 0 at runtime
Index out of bounds — access array[5] when max index is 4
File not found — attempt to open a non-existent file
Type mismatch — passing wrong data type to a function
Stack overflow — infinite recursion
WITHOUT EXCEPTION HANDLING
Program crashes abruptly — user sees cryptic error message
Unsaved data may be lost
Resources (open files, network connections) may not be released
WITH EXCEPTION HANDLING
Program catches the exception and recovers gracefully
User shown a meaningful error message
Program continues running or shuts down cleanly
CAIE Pseudocode — TRY-EXCEPT-ENDTRY

Catching Exceptions in Pseudocode

// CAIE 9618 exception handling syntax
TRY
// Code that might raise an exception
OPENFILE "data.txt" FOR READ
READFILE "data.txt", LineData
Result ← 100 / Divisor // might ÷ by 0
EXCEPT
// Code that runs if exception occurs
OUTPUT "An error occurred"
Result ← 0 // safe default
ENDTRY

// CAIE does not name exception types
// in pseudocode — just TRY/EXCEPT/ENDTRY
HOW IT WORKS
TRY block — contains the code that might raise an exception; executes normally if no exception
EXCEPT block — only executes if an exception was raised in the TRY block; handles the error
If no exception: EXCEPT block is skipped completely
Execution continues after ENDTRY regardless
BENEFITS
Separates error-handling code from normal logic — cleaner, more readable programs
Program continues after catching exception — graceful degradation
Exceptions can propagate up the call stack — handled at the most appropriate level
Exam Practice

Cambridge-style questions

Question 1
A program reads an integer from the user and divides 1000 by that integer. Write pseudocode using TRY-EXCEPT-ENDTRY to handle the case where the user enters zero. State what the EXCEPT block should do. [4]
1
TRY block contains: input reading and the division statement e.g. INPUT n; Result ← 1000 / n
1
EXCEPT block follows immediately — executes ONLY if an exception (division by zero) occurs in the TRY block.
1
EXCEPT block outputs a meaningful message e.g. OUTPUT "Error: cannot divide by zero"
1
ENDTRY closes the construct; program continues execution after ENDTRY — it does not crash. Award mark for noting program continues gracefully.
Common Mistakes

Don't lose easy marks

1
Writing an IF statement instead of TRY-EXCEPT to handle division by zero — IF checks for the condition BEFORE it occurs; TRY-EXCEPT catches the exception AFTER it is raised by the runtime. CAIE exam questions specifically require TRY-EXCEPT-ENDTRY when asking about exception handling.
2
Putting ALL code inside the TRY block — only code that might raise an exception should be in TRY. Recovery code and normal flow should go in EXCEPT or after ENDTRY. Putting everything in TRY makes error handling harder to reason about.
3
Saying "exceptions are the same as syntax errors" — syntax errors are caught at compile time and prevent the program from running. Exceptions occur at runtime during execution. They are fundamentally different in when and how they are detected.
Topic Summary — 4.2.3

What You Need to Know

WHAT IS AN EXCEPTION?
Runtime event disrupting normal flow. Examples: division by zero, index out of bounds, file not found. Different from syntax errors (compile time) and logic errors.
TRY-EXCEPT-ENDTRY
TRY block: code that might fail. EXCEPT block: only runs if exception raised. ENDTRY: resumes normal flow. Program continues gracefully after exception is caught.
BENEFITS
Graceful degradation (no crash). Separates error logic from normal logic. Meaningful user messages. Allows resource cleanup. Exceptions can propagate up the call stack to be handled at the right level.
CSZone

Next Video

4.2.4
File Handling & Streams
OPENFILE · READFILE · WRITEFILE · CLOSEFILE
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools