Saturday, November 16, 2024
Google search engine
HomeLanguagesHandling TypeError Exception in Python

Handling TypeError Exception in Python

TypeError is one among the several standard Python exceptions. TypeError is raised whenever an operation is performed on an incorrect/unsupported object type. For example, using the + (addition) operator on a string and an integer value will raise a TypeError.

Examples

The general causes for TypeError being raised are:

1. Unsupported Operation Between Two Types

In the following example, the variable ‘geek’ is a string, and the variable ‘num’ is an integer. The + (addition) operator cannot be used between these two types and hence TypeError is raised.

Python3




geek = "Geeks"
num = 4
print(geek + num + geek)


Output :

TypeError: must be str, not int


2. Calling a non-callable Identifier

In the below example code, the variable ‘geek’ is a string and is non-callable in this context. Since it is called in the print statement, TypeError is raised.

Python3




geek = "Lazyroar"
print(geek())


Output :

TypeError: 'str' object is not callable


3. Incorrect type of List Index

In Python, list indices must always be an integer value. Since the index value used in the following code is a string, it raises TypeError.

Python3




geeky_list = ["geek", "Lazyroar", "geeky", "geekgod"]
index = "1"
print(geeky_list[index])


Output :

TypeError: list indices must be integers or slices, not str


4. Iterating Through a non-iterative Identifier

In the following code, the value 1234.567890 is a floating-point number and hence it is non-iterative. Forcing Python to iterate on a non-iterative identifier will raise TypeError.

Python3




for geek in 1234.567890:
    print(geek)


Output :

TypeError: 'float' object is not iterable


5. Passing an Argument of the Wrong Type to a Function

In the below code, subtraction function performs difference operation between two arguments of same type. But while calling the function if we pass arguments of two different type then Type error will be thrown by interpreter.

Python3




def subtraction(num1, num2):
    print(num1-num2)
 
 
subtraction('a', 1)


Output:

Hangup (SIGHUP)

Traceback (most recent call last):
File "Solution.py", line 5, in <module>
subtraction('a', 1)
File "Solution.py", line 2, in subtraction
print(num1-num2)
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Handling TypeError

TypeErrors are raised mostly in situations where the programmer fails to check the type of object before performing an operation on them. They can be handled specifically by mentioning them in the except block. In the following example, when one of the indices is found to be an incorrect type, an exception is raised and handled by the program.

Python3




geeky_list = ["Geeky", "Lazyroar", "SuperGeek", "Geek"]
indices = [0, 1, "2", 3]
for i in range(len(indices)):
    try:
        print(geeky_list[indices[i]])
    except TypeError:
        print("TypeError: Check list of indices")


Output :

Geeky
Lazyroar
TypeError: Check list of indices
Geek


RELATED ARTICLES

Most Popular

Recent Comments