Files and Exception Handling (Cs A Level)
Files and Exception Handling (Cs A Level)
Files and Exception Handling (Cs A Level)
FILE PROCESSING
Note: General knowledge on Files in paper 2 is a must
TYPES OF FILES
Binary Files
1. .dat is the extension
2. Data is stored in binary format directly
3. Can be accessed directly
Text Files
1. .txt is the extension
2. These are accessed sequentially
1 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
RANDOM FILES
RANDOM FILES
2 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
RANDOM FILES
Note: The SEEK command moves the file pointer to a given location
SEEK <filename>, <address> Goes to the given address
Note: The command GETRECORD is used to read the record at the file pointer
GETRECORD <filename>, <identifier> get a particular record
Note2: When GETRECORD command is executed, the variable is assigned to the
record that is read
Note: The command PUTRECORD is used to write a record into the file at the file
pointer
PUTRECORD <Filename>, <Identifier> Put a particular record into the
file
3
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
RANDOM FILES
PSEUDOCODE EXAMPLE
TYPE Book
DECLARE BOOKID : INTEGER
DECLARE BOOK_TITLE : STRING
DECLARE AUTHOR : STRING
ENDTYPE
novel.BOOKID = 232
novel.BOOK_TITLE = “Understanding Java Programming”
novel.AUTHOR = “Kazibwe Davis”
4
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
RANDOM FILES
PSEUDOCODE EXAMPLE
CLOSEFILE “KIS.dat”
Qtn: Imagine there is a file with 10 records “Random.dat”. Ask from user book ID
and print the book name
5
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
RANDOM FILES
PSEUDOCODE EXAMPLE
CLOSEFILE “Random.dat”
6
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
EXCEPTION HANDLING
EXCEPTION
It is unplanned event and situation causing a crash with in a computer program
EXCEPTION HANDLING
This refers to block of code which is called when a runtime error occurs to avoid
the program from crashing
SITUATIONS WHERE EXCEPTION HANDLING IS REQUIRED
Division by zero
Run-time error
File does not exist
Invalid array index
Hardware failure
7
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618
EXCEPTION HANDLING
BENEFITS OF USING EXCEPTION HANDLING IN A PROGRAM
1. The program will not crash
2. Results does not cause further error
3. Appropriate error message
4. Exceptional conditions are identified
5. Improve readability
8
Davis_Kazibwe@2023KIS