Error handling‎ > ‎

Type checking (raise)

check variables, stop program and give useful error message


# check if mylist is really a list
mylist=4
if not isinstance(mylist,list):
    raise TypeError('mylist is not a list')
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: Mylist is not a list



# check if 'data' is numberic
data=['A','B']
if not isinstance(data,(int,float)):
    raise TypeError('data is not a number')
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: data is not a number