KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / Python

Sleutelwoord raise

Werpt een BaseException-instantie of geschikte uitzonderingsklasse.

Versie: Python 3.14; repository image, production runtime not attested

Syntax

raise NewError(message) from original

Werpt een BaseException-instantie of geschikte uitzonderingsklasse.

Een losse raise werpt de actieve uitzondering opnieuw en geeft zonder actieve afhandeling RuntimeError. Expliciete from koppelt de oorzaak aan de nieuwe uitzondering.

De conversiefout blijft zo de oorzaak van de toepassingsmelding.

Voorbeelden

def quantity(text):
    try:
        return int(text)
    except ValueError as original:
        raise RuntimeError("invalid quantity") from original
try:
    quantity("two")
except RuntimeError as error:
    print(str(error), type(error.__cause__).__name__)
try:
    raise
except RuntimeError:
    print("no active exception")

Resultaat

invalid quantity ValueError
no active exception

De conversiefout blijft zo de oorzaak van de toepassingsmelding.

Bronnen