Exceptions, assertions

Exceptions

  • Try/catch exceptions
try:
    resultat = numerateur / denominateur

except NameError:
    print("NameError")

except TypeError:
    print("TypeError")

except ZeroDivisionError:
    print("ZeroDivisionError")

# This is done whether there are errors or not
finally:
    print("At least I've tried")
  • Raise exceptions
raise TypeDeLException("message à afficher")

Assertions

def division(x, y):
    assert y != 0, "Can't divide by zero"
    return x / y