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

File Handling &
Exception Handling

OPENFILE · READFILE · WRITEFILE · CLOSEFILE · TRY-EXCEPT

CSZone Cambridge International AS & A Level Computer Science 9618
File Handling Concepts

Sequential File Access in CAIE Pseudocode

Files provide persistent storage — data survives after the program ends. In CAIE pseudocode, files are accessed sequentially (record by record from start to end). Files must be opened before reading/writing and closed afterwards.
FILE MODES
READ — open existing file to read contents.
WRITE — create/overwrite file, write new data.
APPEND — open existing file, add to end without erasing existing content.
KEY FUNCTIONS
OPENFILE — opens a file
READFILE — reads one record into a variable
WRITEFILE — writes a record/line to file
CLOSEFILE — closes the file
EOF() — returns TRUE when end of file reached
Reading from a File

OPENFILE, READFILE, CLOSEFILE

READ ALL RECORDS FROM FILE
DECLARE line : STRING
OPENFILE "data.txt" FOR READ
WHILE NOT EOF("data.txt") DO
READFILE "data.txt", line
OUTPUT line
ENDWHILE
CLOSEFILE "data.txt"
KEY POINTS
Always use WHILE NOT EOF() loop to read all records. READFILE reads ONE record at a time. Always CLOSEFILE after use — releases file handle, ensures data is flushed. File must exist when opened FOR READ.
Writing and Appending to a File

WRITE and APPEND Modes

WRITE MODE (overwrites!)
OPENFILE "output.txt" FOR WRITE
WRITEFILE "output.txt", "Line 1"
WRITEFILE "output.txt", "Line 2"
CLOSEFILE "output.txt"
⚠ Creates new file or overwrites existing!
APPEND MODE (adds to end)
OPENFILE "log.txt" FOR APPEND
WRITEFILE "log.txt", "New entry"
CLOSEFILE "log.txt"
✓ Preserves existing content, adds at end
Exception Handling

TRY…EXCEPT…ENDTRY

An exception is a runtime error (e.g. file not found, division by zero, invalid input). Exception handling allows a program to respond gracefully rather than crash.
// CAIE pseudocode exception handling:
TRY
OPENFILE "data.txt" FOR READ
READFILE "data.txt", line
OUTPUT line
CLOSEFILE "data.txt"
EXCEPT
OUTPUT "Error: file not found"
ENDTRY
COMMON EXCEPTIONS
File not found (read mode). Division by zero. Invalid type conversion (STR→INT on non-numeric string). Index out of bounds. Network/IO timeout.
WHY USE EXCEPTION HANDLING?
Prevents program crash. Provides meaningful error messages. Allows recovery (retry, default value, log error). Makes programs robust and user-friendly.
Exam Practice

Cambridge-style questions

Question 1
Write pseudocode to open a file called "scores.txt" for reading, read all records and output each one, then close the file. Include appropriate exception handling.
6 marks
DECLARE line : STRING
TRY // 1 mark
OPENFILE "scores.txt" FOR READ // 1 mark
WHILE NOT EOF("scores.txt") DO // 1 mark
READFILE "scores.txt", line // 1 mark
OUTPUT line
ENDWHILE
CLOSEFILE "scores.txt" // 1 mark
EXCEPT
OUTPUT "Error reading file" // 1 mark
ENDTRY
Common Mistakes

Don't lose easy marks

1
Forgetting to CLOSEFILE — always close a file after use. Not closing can cause data loss (unflushed writes), resource leaks, and other programs being unable to access the file. The examiner always checks for CLOSEFILE.
2
Using WRITE mode when you want to add to an existing file — WRITE mode destroys existing content! Use APPEND mode when you want to add records to a file that already has data. This is a very common mix-up in exam answers.
3
Reading from a file without an EOF check — reading past the end of file causes a runtime error. Always use WHILE NOT EOF() to safely read all records. Don't assume you know how many records are in the file.
Topic Summary — 2.4.3

What You Need to Know

FILE HANDLING
READ: OPENFILE → WHILE NOT EOF() → READFILE → CLOSEFILE
WRITE: overwrites entire file
APPEND: adds to end of existing file
Always CLOSEFILE after use!
EXCEPTION HANDLING
TRY: attempt risky code
EXCEPT: handle the error gracefully
ENDTRY: closes the block
Use for: file errors, division/0, bad input
CSZone

Next Video

2.5.1
Testing Strategies
Black Box · White Box · Boundary · Test Data Types
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools