Naar hoofdinhoud
🔒 Voorbeeldmodus. De eerste vijftien Foundations-lessen zijn gratis; deze is Pro. Start een 7-daagse trial om de editor, AI-hints en de rest van het curriculum te ontgrendelen. Kaart vereist, op elk moment opzegbaar in Dashboard.Start 7-daagse trial →
← CursussenSenior Deep-DivesModule 9 · Error handling depthCustom exception hierarchies — most-specific except firstpredict134 / 161
+150 XP
Opdracht
📝 **Taak:** Voorspel de exacte uitvoer van 5 regels. De functie handle() probeert elke uitzondering op een geordende behalve-keten. De keten is opzettelijk gerangschikt met de diepste subklasse bovenaan. Traceer welke clausule elk van de vijf testingangen opvangt. 📋 Implementeer bovenstaande functie. Tests worden automatisch uitgevoerd. 💡 **Hint:** Herlees de theorie als je vastloopt.
Voorspel uitvoer

Lees de code zorgvuldig

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")))

Wat zal het programma uitprinten? Schrijf hier:

💬 Discussie

Wees de eerste — stel een vraag of deel een tip.
Log in om mee te doen aan de discussie. Lezen is gratis.
Discussie laden…