Catch TypeError Exception in Python



TypeErrors are caused by combining the wrong type of objects, or calling a function with the wrong type of object.

Example

import sys
try :
ny = 'Statue of Liberty'
my_list = [3, 4, 5, 8, 9]
print  my_list + ny
except TypeError as e:
print e
print sys.exc_type

Output

can only concatenate list (not ""str") to list
<type 'exceptions.TypeError'>
Updated on: 2020-02-12T10:51:23+05:30

644 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements