Python TypeError Exception
Example
A TypeError
occurs if you try to concatenate a string and a number:
x = "hello"
y = 15
print(x + y)
Try it Yourself »
Definition and Usage
The TypeError
exception occurs if an operation
tries to perform an action with an unexpected data type.
You can handle the TypeError
in a try...except
statement, see the example below.
Exception Handling
Example
Handling the TypeError
in a try...except
statement:
try:
x = "hello" + 15
except
TypeError:
print("Please
convert to string before concatenate")
except:
print("Something else went wrong")
Try it Yourself »