1# You can raise a error in python by using the raise keyword
2raise Exception("A error occured!")
1# Raise is used to cause an error
2raise(Exception("Put whatever you want here!"))
3raise(TypeError)
1>>> def catch():
2... try:
3... asd()
4... except Exception as e:
5... print e.message, e.args
6...
7>>> catch()
8global name 'asd' is not defined ("global name 'asd' is not defined",)
1def prefill(n,v):
2 try:
3 n = int(n)
4 except ValueError:
5 raise TypeError("{0} is invalid".format(n))
6 else:
7 return [v] * n
1 # this raises a "NameError"
2
3>>> raise NameError('HiThere')
4
5Traceback (most recent call last):
6 File "<stdin>", line 1, in <module>
7NameError: HiThere