June 2002
Beginner to intermediate
640 pages
14h 24m
English
A raise statement forces an exception to be raised. One form is
raise exception-expression, description-expression
In our denominator example, it raises a specific exception.
>>> raise ZeroDivisionError, "Sequence has a Zero value in the denominator" Traceback (innermost last): File "<stdin>", line 1, in ? ZeroDivisionError: Sequence has a Zero value in the denominator
The exception-expression must be of type String or be an instance object. The description-expression is optional. If you use raise with no exception-expression, it reraises the last exception.
You can easily define your own exception using a string variable.
>>> badListForm = 'badListForm' >>> try: ... raise badListForm, "The list had a malformed tuple" ...
Read now
Unlock full access