Courseiva
Exceptions and File I/O →mediumMultiple Choice

PCAP Exceptions and File I/O Practice Question

A developer has a function that performs several operations and may raise different exceptions. The developer wants to catch a specific exception and then re-raise it after logging. Which code snippet correctly re-raises the same exception without losing its traceback?

⚠ Common exam trap

Python Institute often tests the distinction between a bare `raise` (which preserves the original exception and traceback) and `raise SomeException(...)` (which creates a new exception and loses the original context), leading candidates to incorrectly choose the latter when they think they need to 're-raise' by specifying the exception class again.

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

✓

except ValueError as e: log.error(e); raise

Using a bare `raise` statement inside an `except` block re-raises the original exception instance, preserving its full traceback. This is the standard Python idiom for logging an exception and then propagating it unchanged, ensuring that the original call stack and error context are not lost.

Answer analysis

Option-by-option breakdown

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

  • ✓

    except ValueError as e: log.error(e); raise

    Why this is correct

    The bare `raise` inside the `except` block re-raises the identical exception object that was caught, preserving its full traceback, original message, and any associated attributes. Logging first allows the current layer to record the failure, then the same exception continues propagating up the call stack for the caller to handle. This is the canonical way to log-and-rethrow without altering the exception's identity or stack trace, making the failure visible at every layer that observes it.

  • ✗

    except ValueError as e: log.error(e); return None

    Why it's wrong here

    Returning `None` turns an exception into a normal, successful return, so the caller has no signal that a failure occurred. The exception is handled and swallowed, meaning any code that expects a valid result may silently receive `None` and potentially fail later with a confusing attribute error. This violates fail-fast principles—the error is logged, but the program's state is not preserved, and the exception's propagation is deliberately cut short.

  • ✗

    except ValueError as e: log.error(e); raise ValueError('New')

    Why it's wrong here

    Raising `ValueError('New')` constructs a brand-new exception object, discarding the original exception's message, traceback, and any payload. Unless chained with `from e`, the new exception is not linked to its cause, so debugging the root issue becomes much harder. Even if the type is the same, the original context is lost, which can mislead developers who only see the new message and never know the underlying error that triggered the handler.

  • ✗

    except ValueError as e: log.error(e); pass

    Why it's wrong here

    Using `pass` in the `except` block silently swallows the exception with no logging and no propagation, making the failure completely invisible to the caller and to logs. This is the most dangerous option because it not only changes control flow but also provides zero diagnostic record, so an intermittent error may go unnoticed for a long time. The program continues as if nothing happened, which can lead to corrupted state or unobserved data loss far downstream from the actual fault.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.