When you try to divide a number by zero (0
) in Python, you will get a ZeroDivisionError
.
In Python, division by zero (0
) is not allowed.
number = 20
division = number / 0
print(division)
division = number / 0
ZeroDivisionError: division by zero
In the above example, we tried to divide a number by zero (0
), but we got zero division by error.
To catch zero division by error exception use, try
-except
block.
try:
number = 20
division = number / 0
print(division)
except ZeroDivisionError as e:
print("Exception:", e)
Exception: division by zero
A TypeError
occurs when an operation is performed on an object of an inappropriate type.
This can happen when you try to perform arithmetic or logical operations on incompatible data types.
number_1 = 20
number_2 = "2"
print(number_1 + number_2)
print(number_1 + number_2)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
This error occurs because Python cannot add an integer to a string.
To catch this exception, use try
-except
block while adding numbers.
try:
number_1 = 20
number_2 = "2"
print(number_1 + number_2)
except TypeError as e:
print("Exception:", e)
Exception: unsupported operand type(s) for +: 'int' and 'str'
A NameError
occurs when you try to use a variable that has not been defined.
This can happen if you have misspelled the variable name, or if you have not assigned a value to the variable before trying to use it.
print(number_1)
NameError: name 'number_1' is not defined
In the above example, we have not defined the number_1
variable, so we got a NameError
exception.
To avoid NameError
, you should always make sure that you have defined a variable before trying to use it.
Also, make sure that you have spelt the variable name correctly.
try:
print(number_1)
except NameError as e:
print("Exception:", e)
Exception: name 'number_1' is not defined
An index error in Python occurs when you try to access an element of a list
, tuple
, or string
using an invalid index.
The index is the number that identifies the position of an element in a sequence.
In Python, indexing starts from 0, so the first element in a sequence has an index of 0, the second element has an index of 1, and so on.
colors_list = ["Red", "Blue", "Green"]
print(colors_list[3])
IndexError: list index out of range
In the above example, we tried to access the 3rd
index item, but the list doesn't have an index 3rd
item, so we got the IndexError
.
Handle IndexError
using try
-except
block.
try:
colors_list = ["Red", "Blue", "Green"]
print(colors_list[3])
except IndexError as e:
print("Exception:", e)
Exception: list index out of range
An AssertionError
in Python is raised when an assert
statement evaluates to False
.
The assert
statement is a debugging tool that allows you to check for certain conditions in your code and raise an exception if those conditions are not met.
This can be useful for catching errors early on before they cause your program to crash.
assert 10 > 20
assert 10 > 20
AssertionError
AssertionError
can be handled using a try
-except
block.
try:
assert 10 > 20
except AssertionError as e:
print("Exception: Assertion error")
Exception: Assertion error