First page Back Continue Last page Overview Graphics

Raising Exceptions

The raise statement allows the programmer to raise an exception if an error occurs:

>>> raise SyntaxError("Text of error message")

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

SyntaxError: Text of error message

Example:

try:

raise NameError('Hi There')

except NameError:

print('An exception flew by!')

raise