Courseiva
Question 117 of 169
Exceptions and File I/OhardMultiple ChoiceObjective-mapped

PCAP Exceptions and File I/O Practice Question

A senior developer in a team argues that using try-except blocks is slower than checking conditions with if statements. They propose replacing all try blocks that handle file I/O errors with existence checks using os.path.exists before opening files. During a code review, you recall that Python's official documentation and best practices prefer EAFP (Easier to Ask for Forgiveness than Permission) over LBYL (Look Before You Leap) in many cases, especially in concurrent environments. The team's application is a multi-threaded web server that serves static files from a shared directory. Which is the strongest counterargument against the senior developer's proposal?

⚠ Common exam trap

Python Institute often tests the misconception that try-except is purely about style or performance, when in reality the critical exam point is that LBYL introduces race conditions in concurrent code, making EAFP the safer and recommended pattern.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

LBYL leads to race conditions in concurrent code because the file's state can change between the check and the use.

In a multi-threaded web server, the LBYL approach (checking with os.path.exists) introduces a classic TOCTOU (Time of Check, Time of Use) race condition: between the existence check and the actual file open, another thread could delete or rename the file, causing the open to fail despite the check passing. Python's EAFP idiom (try-except) avoids this window by attempting the operation directly and handling the exception if it fails, which is inherently atomic with respect to the file system state. This is why official Python documentation recommends EAFP over LBYL in concurrent environments.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • if statements are harder to read and maintain.

    Why it's wrong here

    This is a subjective claim rather than a technical one: an if-based LBYL check is often perfectly readable, e.g., `if os.path.exists(path): open(path)`. Readability depends on context and does not address the real problem: in multithreaded code, the file can disappear or change between the `if` check and the subsequent open, causing a race. Thus, even if it were less readable, that would not outweigh the fundamental correctness flaw of LBYL.

  • try-except can catch multiple exception types more cleanly.

    Why it's wrong here

    While EAFP's try-except syntax can indeed catch multiple exception types in one handler, that convenience is orthogonal to the race condition argument. In concurrent code, the exception from a failed open is handled after the fact, but the failure still occurs; LBYL would have attempted to avoid it. So this option highlights a stylistic benefit, not a correction of the TOCTOU risk that the senior developer's proposal actually creates.

  • try-except blocks have no performance cost at all.

    Why it's wrong here

    In CPython, entering a try block has a small but measurable setup cost, and raising an exception is relatively expensive since it involves constructing the exception object and unwinding the stack. However, performance is not the core issue: the option's premise is factually shaky, and even if try-except were free, the decisive flaw in the LBYL proposal remains the race window between check and use. Therefore, this objection misses the technical point.

  • LBYL leads to race conditions in concurrent code because the file's state can change between the check and the use.

    Why this is correct

    This is the classic time-of-check-to-time-of-use (TOCTOU) race: in a multithreaded server, two threads can evaluate `os.path.exists(path)` at nearly the same instant, and then one thread may delete or replace the file before the other actually opens it. The check and the use are not atomic, so LBYL gives a false sense of safety. EAFP, by contrast, wraps the open itself in a try-except, handling the failure exactly when it occurs and eliminating the gap.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.