Revision Notes
Cambridge A Level Computer Science (9618): File Processing and Exception Handling – Revision Notes
Condensed 9618 notes on file modes, serial, sequential and random file operations, exceptions and when to use exception handling, with a self-test.
- Subject
- Computer Science
- Level
- A LEVEL
- Topic
- Topic 20: Further Programming
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to Cambridge A Level Computer Science (9618), 2026. Official specification .
Syllabus page (what it covers and how it is assessed): Cambridge A Level Computer Science.
Syllabus points this page covers
9618 (A Level)
- 20.2 File Processing and Exception Handling
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (9618).
These notes condense section 20.2 “File Processing and Exception Handling” of Cambridge International AS & A Level Computer Science 9618: opening files in read, write and append mode and closing them, reading and writing records, operations on serial, sequential and random files, and understanding and writing exception handling. It is assessed in Paper 3 (pseudocode) and Paper 4 (program code).
Related: File processing and exception handling study guide · File processing and exception handling practice questions · Topic 20 Further Programming revision notes
Key terms
| Term | Meaning |
|---|---|
| Record | A set of related fields about one item |
| Key field | Field that identifies a record uniquely |
| Serial file | Records stored in the order they were added |
| Sequential file | Records stored in order of the key field |
| Random (direct-access) file | Fixed-length records at positions calculated from the key |
| Hashing algorithm | Calculation that turns a key into a record address |
| Collision | Two keys giving the same address |
| Exception | Unexpected event at run time that disrupts the program’s normal flow |
| Exception handling | Code that catches an exception and deals with it so the program can continue |
File modes
| Mode | Use | Warning |
|---|---|---|
| READ | Read existing data from the start | File must exist |
| WRITE | Create a new file | Destroys existing contents |
| APPEND | Add to the end | Keeps existing contents |
| RANDOM | Read or write any record by position | Records must be fixed length |
Pseudocode patterns
Read every line
OPENFILE "F.txt" FOR READ
WHILE NOT EOF("F.txt")
READFILE "F.txt", Line
// process Line
ENDWHILE
CLOSEFILE "F.txt"
Add to a serial file: OPENFILE FOR APPEND → WRITEFILE → CLOSEFILE.
Random file update: calculate address → SEEK → GETRECORD → check key → change fields → SEEK again → PUTRECORD → CLOSEFILE.
Operations by file type
| Operation | Serial | Sequential | Random |
|---|---|---|---|
| Add | Append at the end | Copy to a new file, writing the new record at its key position (and at the end if it is last) | Hash the key, SEEK, PUTRECORD |
| Delete | Copy all other records to a new file | Copy all other records to a new file | Mark the record as empty / overwrite it |
| Find | Read from the start until found or EOF | Read from the start; stop when found or when a larger key is passed | Hash the key, SEEK, GETRECORD, check the key |
| Amend | Copy with the changed record | Copy with the changed record | Read, change, SEEK back, PUTRECORD |
Sequential insertion checklist: flag to stop writing the new record twice; write it before the first larger key; write it after the loop if never written; close both files; replace the old file with the new one.
Exceptions
Examples: file not found; reading past end of file; disk full; text that cannot be converted to a number; division by zero; lost network connection; array index out of range.
Why handle them
- The program does not crash.
- Files are closed and data saved.
- The program can recover (ask again, skip, use a default).
- Error handling is separated from the main logic.
When appropriate: operations depending on things outside the program’s control (files, devices, networks, data from outside). Not a replacement for validating predictable input, and not a fix for logic errors.
Structure
| Language | Form |
|---|---|
| Python | try: … except ValueError: … finally: |
| Java | try { … } catch (Exception e) { … } finally { … } |
| Visual Basic | Try … Catch ex As Exception … Finally … End Try |
Put only the statements that can fail inside the try block; give a helpful message in the handler; close files in finally.
Exam traps
- FOR WRITE used to add records, wiping the file.
- READFILE before the EOF test.
- Forgetting the “insert at end” case for a sequential file.
- Only one file opened for a sequential insertion or deletion.
- No second SEEK before PUTRECORD.
- Key not checked after reading a hashed address.
- Exception handling used for a menu-range check.
- A handler that catches everything silently, hiding faults.
Self-test
- What happens to existing data when a file is opened FOR WRITE?
- Which mode adds a record to the end of a serial file?
- Why must EOF be tested before READFILE in a loop?
- Why does inserting into a sequential file need a second file?
- A product key 1,234 is hashed with key MOD 200. What is the address?
- Why is the key checked after GETRECORD?
- Why is SEEK used again before PUTRECORD?
- Define an exception.
- Give two examples of exceptions in file processing.
- Give two reasons why exception handling is important.
- Should a check that a menu choice is between 1 and 4 use exception handling? Why?
- What is a finally block used for?
Answers
- It is lost: the file is created afresh.
- APPEND.
- So an empty file is not read and the last record is not processed twice or read past the end.
- Lines cannot be inserted into the middle of a text file, so records are copied to a new file with the new one written in its place.
-
- Another record may have hashed to the same address (a collision), so this may not be the record wanted.
- GETRECORD moves the position on; SEEK returns to the record’s address so it is overwritten.
- An unexpected event during run time that disrupts the normal flow of the program.
- Any two: file not found; reading past the end; disk full; data in the file that cannot be converted.
- Any two: stops the program crashing; lets files be closed and data saved; allows recovery; separates error code from main logic.
- No: it is predictable input, so a simple IF validation check is appropriate.
- Code that runs whether or not an exception occurred, such as closing a file.
Related resources
-
Study guides
Cambridge A Level Computer Science (9618): Programming Paradigms
Study guide to 9618 section 20.1: comparing paradigms, addressing modes traced in memory, indirect loops, designing classes, and writing facts, rules and goals.
Computer Science · Cambridge · A Level
Read guide -
Practice questions
Cambridge A Level Computer Science (9618): Programming Paradigms – Practice Questions
Original 9618 practice questions with answers on addressing modes, low-level traces and code, OOP class design and declarative facts, rules and goals.
Computer Science · Cambridge · A Level
Try questions -
Revision notes
Cambridge A Level Computer Science (9618): Programming Paradigms – Revision Notes
Condensed 9618 notes on the four paradigms: addressing modes, imperative features, OOP terms and class design, and declarative facts, rules and goals.
Computer Science · Cambridge · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.