Cara menggunakan python raise typeerror example

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 TypeError.

Show

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=

TypeError: 'str' object is not callable
0

TypeError: 'str' object is not callable
1
TypeError: 'str' object is not callable
2
TypeError: 'str' object is not callable
3 num
TypeError: 'str' object is not callable
3
TypeError: 'str' object is not callable
6

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=

TypeError: 'str' object is not callable
9

TypeError: 'str' object is not callable
1
TypeError: list indices must be integers or slices, not str
1

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




TypeError: list indices must be integers or slices, not str
2=
TypeError: list indices must be integers or slices, not str
4
TypeError: list indices must be integers or slices, not str
5
TypeError: list indices must be integers or slices, not str
6
TypeError: 'str' object is not callable
9
TypeError: list indices must be integers or slices, not str
6
TypeError: list indices must be integers or slices, not str
9
TypeError: list indices must be integers or slices, not str
6
TypeError: 'float' object is not iterable
1
TypeError: 'float' object is not iterable
2

TypeError: 'float' object is not iterable
3=
TypeError: 'float' object is not iterable
5

TypeError: 'str' object is not callable
1
TypeError: 'float' object is not iterable
7

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




TypeError: 'float' object is not iterable
8 geek
Geeky
GeeksforGeeks
TypeError: Check list of indices
Geek
0
Geeky
GeeksforGeeks
TypeError: Check list of indices
Geek
1
Geeky
GeeksforGeeks
TypeError: Check list of indices
Geek
2

Geeky
GeeksforGeeks
TypeError: Check list of indices
Geek
3
TypeError: 'str' object is not callable
1
Geeky
GeeksforGeeks
TypeError: Check list of indices
Geek
5

Output :

TypeError: 'float' object is not iterable

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




TypeError: list indices must be integers or slices, not str
2=
TypeError: list indices must be integers or slices, not str
4
Geeky
GeeksforGeeks
TypeError: Check list of indices
Geek
9
TypeError: list indices must be integers or slices, not str
6
TypeError: 'str' object is not callable
9
TypeError: list indices must be integers or slices, not str
6geek3
TypeError: list indices must be integers or slices, not str
6geek5
TypeError: 'float' object is not iterable
2

geek7=

TypeError: list indices must be integers or slices, not str
4=0
TypeError: list indices must be integers or slices, not str
6=2
TypeError: list indices must be integers or slices, not str
6=4
TypeError: list indices must be integers or slices, not str
6=6
TypeError: 'float' object is not iterable
2