Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to catch TypeError Exception in Python?

In this code, I get an error

ny = 'Statue of Liberty'
my_list = [3, 4, 5, 8, 9]
print  my_list + ny

How to handle this exception and know its type?


1 Answer
Manogna

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

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'>

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.