Courseiva
Question 50 of 169
Exceptions and File I/OhardMultiple ChoiceObjective-mapped

Try-Except-Else-Finally Execution Order in Python — Complete Guide

What is the output of the following code?

try:

exec('1/0')

except:
    print('error')

else:

print('no error')

finally:

print('done')

Quick Answer

The answer is error done because the `exec('1/0')` statement raises a `ZeroDivisionError`, which is caught by the bare `except:` clause, printing 'error', while the `else` clause is skipped entirely since an exception occurred, but the `finally` clause always executes unconditionally, printing 'done'. This demonstrates the strict execution order of the try-except-else-finally construct: the `try` block runs first; if an exception is raised, the matching `except` block runs and the `else` block is bypassed; regardless of outcome, the `finally` block executes last. On the Certified Associate Python Programmer PCAP exam, this pattern tests your understanding of exception handling flow, often appearing in questions that hide a raised exception inside `exec()` or a function call to mislead you. A common trap is assuming `else` runs when any exception occurs, but it only runs if the `try` block completes without an exception. Remember the mnemonic: “Try, except if bad, else if good, finally always.”

⚠ Common exam trap

Python Institute often tests the order of execution in exception handling, specifically that `finally` always runs after `except` (not before), and that `else` is skipped when an exception occurs, causing candidates to misorder the output or forget the `finally` block.

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

error done

The `exec('1/0')` raises a `ZeroDivisionError`, which is caught by the bare `except:` clause, printing 'error'. The `else` clause is skipped because an exception occurred, but the `finally` clause always executes, printing 'done'. Thus the output is 'error' followed by 'done'.

Answer analysis

Option-by-option breakdown

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

  • error done

    Why this is correct

    Correct order.

  • done error

    Why it's wrong here

    Except runs before finally.

  • error

    Why it's wrong here

    Finally also runs.

  • done

    Why it's wrong here

    Except also prints.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, 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 PCAP

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. What is the output of the following code? try: print(1/0) except: print('err') finally: print('fin')

medium
  • A.fin err
  • B.fin
  • C.err fin
  • D.err

Why C: When a division by zero occurs, Python raises a ZeroDivisionError, which is caught by the bare except clause, printing 'err'. The finally clause always executes after the except block, printing 'fin'. Thus the output is 'err' followed by 'fin'.

Variation 2. What will be the output of the following code? try: print(1/0) except: print('error') else: print('no error') finally: print('done')

easy
  • A.no error\ndone
  • B.error\nno error\ndone
  • C.done
  • D.error\ndone

Why D: The code raises a ZeroDivisionError when attempting 1/0, which is caught by the bare except clause, printing 'error'. The else clause is skipped because an exception occurred. The finally clause always executes, printing 'done'. Thus the output is 'error' followed by 'done'.

Variation 3. What is the output of the following code? try: raise ValueError('a') except ValueError as e: print(e.args[0]) finally: print('b')

hard
  • A.a b
  • B.b a
  • C.a\nb
  • D.b

Why C: The `finally` block always executes after the `try` block, even when an exception is raised. The `except` block catches the `ValueError` and prints the first argument of the exception (`'a'`), then the `finally` block prints `'b'`. The output is `a` on one line and `b` on the next, matching `a\nb`.

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.