📁 Paper 1 · 3.2 Programming
3.2.7 & 3.2.9 File Handling & Exception Handling
AQA 8525 · GCSE Computer Science · ~12 min read
Notes
──
Video
──
Worksheet
──
Quiz

3.2.7 File Handling

Programs often need to read data from files (persistent storage) and write data to files. Variables are lost when a program ends — files allow data to persist between runs.

Why use files?

  • Store data permanently (survives when the program closes)
  • Share data between different programs
  • Handle large amounts of data not suited to hard-coded arrays

AQA File Operations

OperationAQA Pseudo-codePurpose
Open for readingopenRead("file.txt")Opens file; reads content
Open for writingopenWrite("file.txt")Opens file; overwrites content
Open for appendingopenAppend("file.txt")Opens file; adds to end
Read a linereadLine(myFile)Returns next line as string
Write a linewriteLine(myFile, text)Writes text + newline
End of file checkendOfFile(myFile)Returns True when no more lines
Close fileclose(myFile)Releases the file resource

Reading from a File

myFile ← openRead("students.txt") WHILE NOT endOfFile(myFile) line ← readLine(myFile) OUTPUT line ENDWHILE close(myFile)

Writing to a File

myFile ← openWrite("scores.txt") writeLine(myFile, "Alice,95") writeLine(myFile, "Bob,78") close(myFile) // ALWAYS close the file

Appending to a File

myFile ← openAppend("log.txt") writeLine(myFile, "New entry added") close(myFile) // openWrite would DELETE existing content; openAppend preserves it

3.2.9 Exception Handling

An exception is a run-time error — something unexpected that causes a program to crash. Exception handling allows your code to detect and respond to errors gracefully, rather than crashing.

Common run-time errors

Error typeExample cause
Division by zeroresult ← 10 DIV 0
File not foundopenRead("missing.txt")
Type mismatchint("abc") — converting non-numeric string
Array out of boundsAccessing index beyond array size

TRY / EXCEPT in AQA

AQA uses TRY and EXCEPT to handle exceptions:

TRY value ← int(INPUT("Enter a number: ")) result ← 100 DIV value OUTPUT result EXCEPT OUTPUT "Error: invalid input or division by zero" ENDTRY

File handling with exception handling

TRY myFile ← openRead("data.txt") line ← readLine(myFile) OUTPUT line close(myFile) EXCEPT OUTPUT "File could not be opened" ENDTRY
Exam tip: Always close files after use — openWrite overwrites the entire file, openAppend adds to it. The WHILE NOT endOfFile() loop is the standard pattern for reading all lines from a file.
⚠️ Common Mistakes
  • Forgetting to close the file after reading or writing
  • Using openWrite when you want to add data — this deletes existing content
  • Not checking endOfFile() in the WHILE condition — causes an error when reading past the end
  • Thinking TRY/EXCEPT prevents all errors — it only handles them at runtime
Video coming soon

Key points

  • openRead, openWrite, openAppend — three file modes
  • WHILE NOT endOfFile() — standard read pattern
  • Always close() files after use
  • TRY/EXCEPT handles run-time errors gracefully
Click slide or press arrow keys to navigate
✍️

Worksheet — 3.2.7 & 3.2.9

8 questions · 22 marks

Q1State the difference between openWrite and openAppend.[2]
✅ Mark scheme
Mark scheme
openWrite overwrites/deletes all existing content and starts from scratch [1]; openAppend keeps existing content and adds new data to the end of the file [1].
Q2Write pseudo-code to open "names.txt" for reading and output every line until the end of the file.[4]
✅ Mark scheme
Mark scheme
myFile ← openRead("names.txt") [1]; WHILE NOT endOfFile(myFile) [1]; OUTPUT readLine(myFile) [1]; ENDWHILE; close(myFile) [1].
Q3Why is it important to close a file after you have finished using it?[2]
✅ Mark scheme
Mark scheme
Any two from: releases system resources [1]; ensures all data is saved/flushed to disk [1]; prevents file corruption [1]; allows other programs to access the file [1] — max 2.
Q4Write pseudo-code to write "Error log entry" to a file "log.txt" without erasing previous entries.[3]
✅ Mark scheme
Mark scheme
myFile ← openAppend("log.txt") [1]; writeLine(myFile, "Error log entry") [1]; close(myFile) [1].
Q5What is an exception in programming? Give one example.[2]
✅ Mark scheme
Mark scheme
An exception is an unexpected error that occurs at run-time / while the program is running [1]; e.g. division by zero, file not found, converting a non-number to integer [1].
Q6Write pseudo-code using TRY/EXCEPT that asks the user for an integer. If the input is invalid, output "Please enter a valid number."[4]
✅ Mark scheme
Mark scheme
TRY [1]; num ← int(INPUT("Enter a number: ")) [1]; EXCEPT [1]; OUTPUT "Please enter a valid number." [1]; ENDTRY.
Q7Explain why exception handling is better than crashing when an error occurs.[2]
✅ Mark scheme
Mark scheme
Exception handling allows the program to continue running / respond gracefully [1]; it can inform the user of the error and allow them to try again rather than losing all work / corrupting data [1].
Q8A program opens "scores.txt" for reading, but the file doesn't exist. Write code using TRY/EXCEPT to handle this gracefully, outputting an appropriate message.[3]
✅ Mark scheme
Mark scheme
TRY [1]; myFile ← openRead("scores.txt"); [reads from file…] EXCEPT [1]; OUTPUT "File not found" / appropriate message [1]; ENDTRY.
Check your answers above.
Topic Quiz
Q 1 of 10
You scored
out of 10
Card 1 of 6
Click to flip
🎉
All done!
TermDefinition
🎯

Mini Test — 3.2.7/9 File Handling

Timed exam conditions.

  • 8 questions · 10 minutes
  • 5 MCQ + 3 short answer
← 3.2.6b 2D Arrays
18 of 57 · AQA 8525
3.2.8 Random Numbers →