Quiz: Exception Handling
Instructions: Choose the best answer for each question. Answers are provided at the end.
Q1. What happens when an exception is raised and there is no try..except block anywhere in the call stack?
A) The exception is silently ignored
B) The program terminates with a runtime error
C) The program continues from the line after the raise statement
D) The compiler prevents this situation
Q2. Which block guarantees that cleanup code executes whether or not an exception occurs?
A) try..except
B) try..finally
C) try..catch
D) begin..end
Q3. What is the output of this code if no exception occurs in the try section of a try..except block?
A) Both the try and except sections execute
B) Only the try section executes; the except section is skipped
C) Only the except section executes
D) Neither section executes
Q4. Which exception class is raised by StrToInt('abc')?
A) ERangeError
B) EInOutError
C) EConvertError
D) EAccessViolation
Q5. What does raise with no arguments do inside an except block?
A) Raises a new generic Exception
B) Re-raises the current exception to propagate it further
C) Terminates the program immediately
D) Causes a compiler error
Q6. In the try..finally block, when does the finally section execute?
A) Only when an exception occurs
B) Only when no exception occurs
C) Always — whether or not an exception occurs
D) Only when Exit is called
Q7. Why is the following code dangerous?
try
ImportantOperation;
except
end;
A) It causes a memory leak
B) It silently ignores all exceptions, hiding bugs and data corruption
C) It catches only EConvertError
D) It raises a new exception
Q8. What is the correct order for multiple on clauses in a try..except block?
A) Most general to most specific B) Most specific to most general C) Alphabetical order D) Order does not matter
Q9. Which of the following is the recommended pattern for managing object lifetimes?
A) Create the object, use it, then Free it without any try blocks B) Create the object inside a try..except block C) Create the object, immediately enter try..finally, Free in the finally section D) Let the garbage collector handle it
Q10. What advantage do custom exception classes provide over using the generic Exception class?
A) They run faster B) They allow callers to catch specific error types and access structured error data C) They prevent all runtime errors D) They are required by the compiler
Q11. In this code, what happens if Reset(F) raises an EInOutError?
AssignFile(F, 'data.txt');
Reset(F);
try
ReadLn(F, Data);
finally
CloseFile(F);
end;
A) CloseFile(F) executes because it is in the finally block
B) CloseFile(F) does not execute because the exception occurs before try
C) The program enters an infinite loop
D) The exception is silently caught
Q12. Which of the following is appropriate use of exceptions?
A) Using exceptions to check if a number is positive
B) Using exceptions to handle a file that might not exist
C) Using exceptions to exit a loop early
D) Using exceptions instead of if statements for all conditionals
Q13. What does Free do when called on a nil object reference?
A) Raises an EAccessViolation
B) Does nothing — it is safe to call Free on nil
C) Frees a random memory location
D) Causes a compiler error
Q14. What is the "write-to-temp-then-rename" pattern?
A) A pattern for handling temporary variables B) Writing data to a temp file, then renaming it to the target — preventing corruption if writing fails C) Creating temporary backup files before every operation D) A pattern for renaming files safely
Q15. How do exceptions propagate through the call stack?
A) They propagate downward to called procedures B) They propagate upward through callers until a handler is found or the program terminates C) They are handled only in the procedure where they are raised D) They propagate to all procedures in the program simultaneously
Answer Key
| Q | Answer | Explanation |
|---|---|---|
| 1 | B | Unhandled exceptions terminate the program with a runtime error message. |
| 2 | B | try..finally guarantees execution of the finally section unconditionally. |
| 3 | B | If no exception occurs, the except section is skipped entirely. |
| 4 | C | StrToInt raises EConvertError when the string is not a valid integer. |
| 5 | B | Bare raise in an except block re-raises the current exception. |
| 6 | C | The finally section always executes: on success, on exception, and on Exit/Break. |
| 7 | B | An empty except block catches and discards all exceptions, hiding bugs. |
| 8 | B | Specific exceptions first; otherwise, a general handler catches everything before specific ones are checked. |
| 9 | C | The Create-Try-Finally pattern ensures the object is freed even if an exception occurs. |
| 10 | B | Custom exceptions allow specific handling and can carry structured data (properties). |
| 11 | B | The exception occurs before the try block, so the finally section does not execute. |
| 12 | B | A missing file is an exceptional condition appropriate for exception handling. |
| 13 | B | TObject.Free checks for nil before calling Destroy, making it safe. |
| 14 | B | This pattern prevents data loss: if writing fails, the original file remains intact. |
| 15 | B | Exceptions propagate upward through callers (unwinding the stack) until a handler is found. |