Exception (Python)

An exception is an error that occurs while a program is running, such as dividing by zero or accessing a missing key. If not handled, it stops the program and prints a traceback.

Python

Common built-in exceptions include ValueError, TypeError, KeyError, and ZeroDivisionError. You handle exceptions with try/except so your program can recover gracefully.

Examples

Python

try:
    print(10 / 0)
except ZeroDivisionError:
    print("can't divide by zero")

Output:

can't divide by zero

Learn it in a lesson

See also

Back to the Dictionary