Courseiva
Exceptions and File I/O →mediumMultiple Select

PCAP Exceptions and File I/O Practice Question

A backup utility writes a manifest with `with open('manifest.txt', 'w') as out:`. The developer needs to ensure that partial writes are not silently left in the file when an exception occurs mid-loop. Which two statements about using a try/except/else/finally structure around this write loop are correct? (Choose two.)

⚠ Common exam trap

The trap here is assuming that a handled exception or an early return cancels the finally suite, when finally is designed to run during every form of exit from the statement.

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

✓

The finally block executes even if an except clause handles the exception and the handler itself raises a new exception.

In a try/except/else/finally statement, else runs only when the try suite finishes without an exception, and finally runs unconditionally during unwinding, including when a handler raises or when try exits via return. Those two guarantees let the developer mark success in else and perform unconditional cleanup in finally, protecting against partially written manifests.

Answer analysis

Option-by-option breakdown

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

  • ✓

    The finally block executes even if an except clause handles the exception and the handler itself raises a new exception.

    Why this is correct

    finally is guaranteed to run during the unwinding of the try statement regardless of whether an exception occurred, was handled, or was replaced by a new exception inside a handler. This guarantee is what makes finally suitable for cleanup such as removing a temporary manifest file or releasing a lock.

  • ✗

    Code in the else block runs before the except clauses are evaluated, so it can change which handler is selected.

    Why it's wrong here

    Ordering is the opposite: except clauses are evaluated as the exception propagates, and else runs only after the try suite completes with no exception at all. The else block never participates in handler selection. This distractor reverses the documented control flow of the try statement.

  • ✗

    A return statement inside the try block prevents the finally block from executing.

    Why it's wrong here

    A return, break, or continue inside try still triggers the finally suite before control actually leaves the statement. The pending return value is saved, finally runs, and then the return completes unless finally itself returns or raises. Cleanup code therefore remains reliable even with early exits.

  • ✓

    The else block runs only when the try block completes without raising an exception.

    Why this is correct

    The else clause of a try statement executes after the try suite finishes normally, meaning no exception propagated. If an exception is raised and handled by an except clause, the else block is skipped. This makes else the right place for code that should run only on success, such as marking the manifest complete.

  • ✗

    If an except clause handles the exception, the finally block is skipped because the error was already resolved.

    Why it's wrong here

    Handling an exception in an except clause does not cancel the finally block. The finally suite still runs after the handler completes, before control leaves the try statement. Assuming otherwise would leave cleanup code unexecuted exactly when it may be most needed, such as deleting an incomplete manifest.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.