A Python script processes a log file line by line. If a line cannot be decoded due to an encoding error, the script should skip the line and continue. Which exception handling approach is best?
Trap 1: Wrap the entire file reading in a try-except block without…
Wrapping the entire file reading in a bare try-except block masks every exception, including genuine programming errors like a NameError or KeyError in your parsing logic, making the script fail silently. Even if a UnicodeDecodeError occurs, the whole read loop aborts on the first bad line, so you cannot skip that line and continue processing the rest of the file. A bare except also catches KeyboardInterrupt and SystemExit, which can make the script impossible to interrupt cleanly.
Trap 2: Use a try-except-else block to handle success and failure…
A try-except-else structure with an explicit exit on failure directly contradicts the stated goal of processing the log file continuously. The else clause only runs if no exception was raised in the try block, so it doesn't help isolate a single decoding failure—the exception would still abort the iteration. Calling sys.exit() or break on the first UnicodeDecodeError discards every subsequent line, which is exactly the opposite of skipping only the malformed record and moving on.
Trap 3: Catch Exception in the outer loop and break on error.
Catching Exception is far too broad because it intercepts not only UnicodeDecodeError but also every other exception type, including programming errors like AttributeError or IndexError that indicate actual bugs in your logic. Adding break on any error means the loop stops permanently at the first problem line, losing all subsequent log entries and defeating the purpose of line-by-line processing. A targeted except UnicodeDecodeError with continue is the only way to both skip malformed lines and keep the script resilient.
- A
Wrap the entire file reading in a try-except block without specifying exception type.
Why it fails: Wrapping the entire file reading in a bare try-except block masks every exception, including genuine programming errors like a NameError or KeyError in your parsing logic, making the script fail silently. Even if a UnicodeDecodeError occurs, the whole read loop aborts on the first bad line, so you cannot skip that line and continue processing the rest of the file. A bare except also catches KeyboardInterrupt and SystemExit, which can make the script impossible to interrupt cleanly.
- B
Use a try-except-else block to handle success and failure separately, exiting on failure.
Why it fails: A try-except-else structure with an explicit exit on failure directly contradicts the stated goal of processing the log file continuously. The else clause only runs if no exception was raised in the try block, so it doesn't help isolate a single decoding failure—the exception would still abort the iteration. Calling sys.exit() or break on the first UnicodeDecodeError discards every subsequent line, which is exactly the opposite of skipping only the malformed record and moving on.
- C
Inside the loop, wrap the line decoding in a try-except UnicodeDecodeError block and continue.
Placing the decoding inside a loop and catching only UnicodeDecodeError is the precise, minimal solution. When a line fails to decode, the except block lets you skip just that iteration via continue, so the script proceeds to the next line without exiting or masking unrelated bugs. This approach also keeps the exception scope narrow, so a programming mistake elsewhere in the loop would still surface normally rather than being silently ignored.
- D
Catch Exception in the outer loop and break on error.
Why it fails: Catching Exception is far too broad because it intercepts not only UnicodeDecodeError but also every other exception type, including programming errors like AttributeError or IndexError that indicate actual bugs in your logic. Adding break on any error means the loop stops permanently at the first problem line, losing all subsequent log entries and defeating the purpose of line-by-line processing. A targeted except UnicodeDecodeError with continue is the only way to both skip malformed lines and keep the script resilient.