Courseiva
Functions, Tuples, Dictionaries and ExceptionshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A critical automation system uses a try-except block to handle errors during file operations. The current code uses a bare except: clause to catch any error and perform cleanup. However, when an operator tries to stop the program with Ctrl+C, the KeyboardInterrupt exception is caught, and the cleanup routine runs, preventing a clean exit. Additionally, if the system runs out of memory, MemoryError is caught. The developers need to modify the exception handling so that system-exiting exceptions (such as KeyboardInterrupt and SystemExit) are not caught, but other exceptions (e.g., FileNotFoundError, PermissionError) are still handled for cleanup. Which modification best achieves this?

⚠ Common exam trap

The PCEP exam often tests the misconception that a bare `except:` is equivalent to `except Exception:`, when in fact it catches all `BaseException` subclasses, including `KeyboardInterrupt` and `SystemExit`, which should typically be allowed to terminate the program.

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

Change the bare except: to except Exception:

Changing the bare `except:` to `except Exception:` ensures that only exceptions inheriting from the built-in `Exception` class are caught. System-exiting exceptions like `KeyboardInterrupt` and `SystemExit` inherit from `BaseException` directly, not from `Exception`, so they will propagate uncaught, allowing a clean exit. This preserves the cleanup behavior for file-related errors such as `FileNotFoundError` and `PermissionError`, which are subclasses of `Exception`.

Answer analysis

Option-by-option breakdown

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

  • Change the bare except: to except Exception:

    Why this is correct

    except Exception: catches all exceptions that inherit from Exception, excluding KeyboardInterrupt and SystemExit.

  • Remove the except block entirely and rely on a finally block for cleanup

    Why it's wrong here

    A finally block alone will run even if no exception occurs, but it does not handle exceptions; the program would still crash.

  • Replace the single except block with multiple specific except blocks for each expected file error

    Why it's wrong here

    This is too restrictive; unexpected exceptions would not be caught and might leave the system in an inconsistent state.

  • Define a custom exception class and raise it for all file errors

    Why it's wrong here

    This does not address the problem of catching critical system exceptions.

About these practice questions

One of 498 original PCEP 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

Same concept, more angles

1 more way this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer is writing a robust script that must handle file reading errors. The script should catch only I/O-related exceptions (e.g., FileNotFoundError, PermissionError) and let other exceptions propagate. Which exception handling structure is best suited?

hard
  • A.Use a single bare except: clause
  • B.Use except: and then re-raise the exception
  • C.Use multiple except blocks for specific exception types
  • D.Use except Exception: as the only handler

Why C: Using multiple `except` blocks for specific exception types (e.g., `FileNotFoundError`, `PermissionError`) allows the script to catch only I/O-related exceptions while letting all other exceptions propagate unhandled. This matches the requirement precisely, as each `except` clause targets a distinct exception class, and any unlisted exception will not be caught, preserving the intended error propagation behavior.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP 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 PCEP exam.