Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 9 · Error handling depthCustom exception hierarchies — most-specific except firstpredict134 / 161
+150 XP
Task
📝 **Task:** Predict the exact 5-line output. The handle() function tries every exception against an ordered except chain. The chain is intentionally arranged with the deepest subclass at the top. Trace which clause catches each of the five test inputs. 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

class AppError(Exception):
    pass


class NotFound(AppError):
    pass


class ValidationError(AppError):
    pass


class FieldError(ValidationError):
    pass


def handle(exc: Exception) -> str:
    try:
        raise exc
    except FieldError:
        return "field"
    except ValidationError:
        return "validation"
    except NotFound:
        return "not-found"
    except AppError:
        return "app"
    except Exception:
        return "unknown"


print(handle(FieldError("email bad")))
print(handle(ValidationError("schema bad")))
print(handle(NotFound("user 42")))
print(handle(AppError("generic")))
print(handle(ValueError("not ours")))

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…