Courseiva
Functions, Tuples, Dictionaries and ExceptionshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

What is the output of the following code?

def f():
    try:

raise ValueError('error1') except ValueError: raise TypeError('error2')

try:

f() except TypeError as e:

print(e)

except ValueError:

print('ValueError')

⚠ Common exam trap

The PCEP exam often tests the misconception that the original exception's message or type will be printed, when in fact the `except` block raises a completely new exception that replaces the original.

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

error2

The code raises a `ValueError` inside the `try` block of function `f()`, which is caught by the `except ValueError` handler. That handler then raises a new `TypeError('error2')`. This new exception propagates out of `f()` and is caught by the outer `except TypeError as e` block, which prints the exception message `'error2'`.

Answer analysis

Option-by-option breakdown

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

  • error2

    Why this is correct

    Correct; the TypeError is raised and caught.

  • Error: unhandled exception

    Why it's wrong here

    The exception is handled.

  • error1

    Why it's wrong here

    Incorrect; the ValueError is caught and replaced by TypeError.

  • ValueError

    Why it's wrong here

    Incorrect; ValueError is not propagated.

About these practice questions

Courseiva writes every PCEP question from scratch — 498 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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 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.