- ZeroDivisionError
A simple exception
When something goes wrong, Python throws (raises) an exception. For example, trying to divide a number by 0 won't work. If the exception is not handled, it will end the execution.
In some programming languages we use the expression "throwing an exception" in other languages the expression is "raising an exception". I use the two expressions interchangeably.
In the next simple example, Python will print the string before the division, then it will throw an exception, printing it to the standard error that is the screen by default. Then the script stops working and the string "after" is not printed.
examples/exceptions/divide_by_zero.py
def div(a, b): print("before") print(a/b) print("after") div(1, 0) # before # Traceback (most recent call last): # File "examples/exceptions/divide_by_zero.py", line 8, in <module> # div(1, 0) # File "examples/exceptions/divide_by_zero.py", line 5, in div # print(a/b) # ZeroDivisionError: integer division or modulo by zero