10.4 Raising Exceptions
The raise statement allows the programmer to force a
specified exception to occur.
For example:
>>> raise NameError, 'HiThere'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: HiThere
The first argument to raise names the exception to be
raised. The optional second argument specifies the exception's
argument.
If you need to determine whether an exception was raised but don't
intend to handle it, a simpler form of the raise statement
allows you to re-raise the exception:
>>> try:
... raise NameError, 'HiThere'
... except NameError:
... print 'An exception flew by!'
... raise
...
An exception flew by!
Traceback (most recent call last):
File "<stdin>", line 2, in ?
NameError: HiThere
|