File handling, exceptions and the ethics of computing

A-Level Computer Science · Recursion, OOP and File Handling

Serial, sequential and random access files

A serial file stores records in the order they arrive — fine for a transaction log, useless for finding one record quickly. A sequential file keeps records in key order, so it can be searched more efficiently and merged. A random access (direct) file computes a record's position from its key using a hashing algorithm, giving near-instant access — the method used when a single record must be retrieved on demand.

Hashing and collisions

A hashing algorithm converts a key into an address. Two keys can hash to the same address — a collision — resolved by placing the record in the next free slot (open addressing) or by chaining records at that address. A good hash spreads records evenly; a poor one produces long chains and destroys the speed advantage.

File operations

Open for read, write or append; read or write a record; close. Forgetting to close a file risks losing data still sitting in the buffer, and opening for write rather than append silently destroys the existing contents.

Exception handling

A run-time error such as a missing file, a division by zero or invalid input should be caught, not allowed to crash the program: TRY … EXCEPT … FINALLY. The FINALLY block runs whether or not an error occurred, which is where files get closed. Catching every exception and doing nothing is worse than not catching it — the program then fails silently with corrupt data.

Ethics and ownership

Copyright protects the expression of a program; licences (proprietary, free, open source) define what users may do. Professional codes of conduct require competence, honesty about limitations, protection of client data, and refusing work beyond one's ability. The exam also asks about the ethics of automated decisions — who is accountable when software trained on biased data refuses someone a loan.

Worked example: hashing, collisions and error handling in a piece of code

A direct-access file has $11$ locations, numbered $0$ to $10$. The hash function is address = student ID MOD 11.

(a) Determine the addresses for the IDs: $4123$, $2085$, $7016$, $3096$.

(b) Write pseudocode to read a record with error handling.

Solution

(a) Calculate each address.

$4123 \bmod 11$: $11\times374=4114$, remainder $9$ ⇒ location 9
$2085 \bmod 11$: $11\times189=2079$, remainder $6$ ⇒ location 6
$7016 \bmod 11$: $11\times637=7007$, remainder $9$ ⇒ location 9: COLLISION
$3096 \bmod 11$: $11\times281=3091$, remainder $5$ ⇒ location 5

Resolving the collision with open addressing: $7016$ is placed in the next empty location, location 10.

Consequence when searching: to find $7016$, the program hashes to location $9$, sees that it holds $4123$ and not the key being sought, so it has to probe on to location $10$. If there are many collisions, the probe chain gets longer and "instant" access gradually becomes linear search; this is why the hash function must spread keys evenly and the table should not be too full.

(b) Pseudocode to read a record with error handling.

TRY
  OPENFILE "SinhVien.dat" FOR RANDOM
  INPUT MaSV
  DiaChi ← MaSV MOD 11
  SEEK "SinhVien.dat", DiaChi
  GETRECORD "SinhVien.dat", BanGhi
  IF BanGhi.Ma <> MaSV THEN
    OUTPUT "Not found at the hashed address, keep probing"
  ELSE
    OUTPUT BanGhi.HoTen
  ENDIF
EXCEPT FileNotFound
  OUTPUT "Could not open the data file"
EXCEPT InvalidInput
  OUTPUT "Student ID must be an integer"
FINALLY
  CLOSEFILE "SinhVien.dat"
ENDTRY

Three details that earn marks. First, there are two separate EXCEPT blocks with different messages; catching every error in one block and printing "an error occurred" leaves the user not knowing what to do. Second, CLOSEFILE is in FINALLY so the file is always closed whether or not there is an error. Third, the program still checks the key of the record it read, because a correct hash address does not guarantee that it is the record being sought.

Common mistake

Opening a file in WRITE mode when you actually want to APPEND. Opening for write empties the file immediately and with no warning at all: all the old data is lost before the program has even written its first line. This is a real data-destroying error, not just an exam error.

Key takeaway

The three file types differ in how a record is found; hashing gives fast access but collisions must be handled. Error handling needs a separate EXCEPT block for each type of error and FINALLY to close the file. And remember professional ethics and responsibility for automated decisions.

← Object-oriented programming · All A-Level Computer Science lessons · Practice A-Level Computer Science questions (free, instant scoring) · Operating systems: scheduling, memory and interrupts →