Which error is an exception?
Which of these is most likely to cause an exception while a program is running?
A an IF statement that has no matching ENDIF
B a loop condition written as Count < 10 when it should be Count <= 10
C opening a file for reading after the file has been deleted by another user
D a variable name that is spelt differently in its declaration and where it is used
[1]
- 1
Classify each error. A missing ENDIF (A) and a misspelt identifier (D) are found by the compiler or interpreter before the program runs: they are syntax (translation) errors. B runs without stopping but gives the wrong result: a logic error.
- 2
C happens only at run time and depends on something outside the program, the file no longer being there. The program cannot carry on normally, so an exception is raised. Answer C (1).
Answer
C
Examiner tip
Examiner tip: an exception is an unexpected event during execution, often caused by something outside the program's control: a missing file, invalid user input, a lost connection to a device. These are exactly the situations where exception handling is appropriate.
Common mistake: choosing B. A logic error never stops the program, so there is nothing for an exception handler to catch.