Python Quiz 1 – Exceptionally Crazy

The Python programming language allows you to catch exceptions using the try / except construct. But what happens if you nest exception handlers and throw in a break statement too?

Your mission in this quiz is to find the answer to that question!

The Quiz

Are you ready to start solving quizzes? Of course, you are!

Here’s your first one: # 01_exception_diversion.py

try:
    for i in range(3):
        try:
            1 / 0
        except ZeroDivisionError:
            raise ZeroDivisionError("Error: You divided by zero!")
        finally:
            print("Finally executed")
            break
except ZeroDivisionError:
    print("Outer ZeroDivisionError exception caught")

What will be output if you run this code?

A) Outer ZeroDivisionError exception caught

B) Error: You divided by zero!

C) Error: You divided by zero! Finally executed

D) Finally executed

E) None of the above

When you are ready, flip to the answer section of this book and check your answer!

Hint

If you’re stuck, try reading about Python’s break statement. It’s not normally used in exception handling.

The Python Quiz Book

This quiz comes from my latest book, The Python Quiz Book. If you’d like to see the answer to this quiz or one of the over one hundred other quizzes, you should get the book! The quiz book will be finished in March 2023. However, you can purchase early access to it on the following websites:

Leave a Comment

Your email address will not be published. Required fields are marked *