Skip to main content
100%

A nice python coding question

✓ Published0🌍 Public
BBBischof
Last edited Apr 27, 2017
Created on Apr 27, 2017

This example demonstrates Python exception matching through a class hierarchy, showing how the interpreter selects the first matching `except` clause. The code defines three exception classes—`B`, `C` inheriting from `B`, and `D` inheriting from `C`—then iterates over them, raising each in turn. For each raised exception, a chain of `except` handlers (catching `D`, then `C`, then `B`) prints the corresponding class name. The output reveals that a raised `D` is caught by the first handler, a raised `C` by the second, and a raised `B` by the third, illustrating that more specific exceptions are matched before their base classes.

AI-generated description

Similar vizzes