A developer writes a function that reads a configuration file and returns its contents as a string. The file might not exist. Which exception should be caught to handle a missing file?
Trap 1: PermissionError
PermissionError is also a subclass of OSError, but it is raised when the operating system denies access to a resource due to insufficient permissions, such as read, write, or execute rights. In the scenario of a missing configuration file, the file is absent rather than being blocked by permissions, so the failure occurs earlier in the lookup process and does not involve access control. Choosing PermissionError would misidentify the root cause and complicate debugging.
Trap 2: IOError
IOError is an alias for OSError in Python 3; it is a broad category that covers many I/O failures, including missing files, permission issues, and device errors. While a missing configuration file would technically trigger an OSError, IOError is too generic and does not convey the specific reason—the file's absence—that a developer needs to handle. Relying on IOError forces the error handler to parse the error code or message to distinguish between failure modes, which is unnecessary when FileNotFoundError exists.
Trap 3: OSError
OSError is the parent class for most operating-system-related errors, including FileNotFoundError, PermissionError, and many others. Catching OSError for a missing configuration file is overly broad and would also catch unrelated errors like disk full or connection reset, masking the actual problem. FileNotFoundError is a more specific subclass, allowing precise handling and clearer error reporting, which is why OSError alone is inappropriate.
- A
FileNotFoundError
FileNotFoundError is a built-in exception raised when a file or directory is requested but cannot be found at the specified path. It is a subclass of OSError and is specifically designed for missing-file scenarios, which is exactly the case when reading a configuration file that does not exist. In Python 3, it is the canonical exception for this situation, replacing the more generic IOError that was used in earlier versions.
- B
PermissionError
Why wrong: PermissionError is also a subclass of OSError, but it is raised when the operating system denies access to a resource due to insufficient permissions, such as read, write, or execute rights. In the scenario of a missing configuration file, the file is absent rather than being blocked by permissions, so the failure occurs earlier in the lookup process and does not involve access control. Choosing PermissionError would misidentify the root cause and complicate debugging.
- C
IOError
Why wrong: IOError is an alias for OSError in Python 3; it is a broad category that covers many I/O failures, including missing files, permission issues, and device errors. While a missing configuration file would technically trigger an OSError, IOError is too generic and does not convey the specific reason—the file's absence—that a developer needs to handle. Relying on IOError forces the error handler to parse the error code or message to distinguish between failure modes, which is unnecessary when FileNotFoundError exists.
- D
OSError
Why wrong: OSError is the parent class for most operating-system-related errors, including FileNotFoundError, PermissionError, and many others. Catching OSError for a missing configuration file is overly broad and would also catch unrelated errors like disk full or connection reset, masking the actual problem. FileNotFoundError is a more specific subclass, allowing precise handling and clearer error reporting, which is why OSError alone is inappropriate.