Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

In a try-except block, a developer has two except clauses: except ValueError: and except: (bare except). If the code in the try block raises a ValueError, which except clause is executed?

⚠ Common exam trap

The PCEP exam often tests the order of except clauses and the fact that Python executes only the first matching handler, leading candidates to mistakenly think both clauses run or that the bare except overrides a specific match.

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 ValueError except clause

When a ValueError is raised in the try block, Python searches the except clauses in the order they appear. It finds the first matching clause, which is `except ValueError:`, and executes it. The bare `except:` clause is only reached if no preceding named except clause matches the exception type.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Both in order

    Why it's wrong here

    Python matches the first except clause whose exception type matches, then exits the handler chain; the bare except is never reached once ValueError is caught. It is tempting because both clauses could logically apply. It would be correct only if the ValueError clause re-raised the exception, allowing the bare except to catch it.

  • ✗

    Neither; program crashes

    Why it's wrong here

    A matching except ValueError clause catches the exception, so the program continues normally rather than crashing. It is tempting because an unhandled exception does terminate the program. It would be correct if no except clause matched the raised exception type, or if the exception were raised outside any try block.

  • ✓

    The ValueError except clause

    Why this is correct

    Python evaluates except clauses in written order, and a specific handler takes precedence over a bare except. Since ValueError matches the named clause first, that handler runs; the bare except only catches exceptions no earlier clause matched.

  • ✗

    The bare except clause

    Why it's wrong here

    Python evaluates except clauses in order and stops at the first match; except ValueError matches before the bare except is considered. It is tempting because a bare except catches everything. It would be correct only if the specific ValueError clause were absent or listed after the bare except.

About these practice questions

One of 482 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

3 more ways 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. Consider the code: try: try: raise TypeError except ValueError: print('A') except TypeError: print('B') finally: print('C'). What is printed?

hard
  • A.A, B, and C
  • B.C only
  • ✓ C.B and C
  • D.A and C

Why C: The inner `try` raises a `TypeError`. The inner `except ValueError` does not catch it, so the exception propagates to the outer `except TypeError`, which catches it and prints 'B'. The `finally` block always executes, printing 'C'. Thus, the output is 'B' and 'C'.

Variation 2. 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')

hard
  • ✓ A.error2
  • B.Error: unhandled exception
  • C.error1
  • D.ValueError

Why A: 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'`.

Variation 3. A developer writes: try: x = int('hello') except ValueError: x = 0 except TypeError: x = -1 finally: x = x + 1 What is the final value of x?

hard
  • A.0
  • ✓ B.1
  • C.-1
  • D.2

Why B: The code attempts to convert the string 'hello' to an integer, which raises a ValueError. The except ValueError block catches this and sets x = 0. The finally block always executes, adding 1 to x, so x becomes 1. The TypeError except is never triggered because the error is a ValueError, not a TypeError.

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.