O'Reilly logo

Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython by Richard Hightower

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The raise Statement

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" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required