10.6. Raising Exceptions

The interpreter was responsible for raising all of the exceptions which we have seen so far. These exist as a result of encountering an error during execution. A programmer writing an API may also wish to throw an exception on erroneous input, for example, so Python provides a mechanism for the programmer to explicitly generate an exception: the raise statement.

10.6.1. raise Statement

The raise statement is quite flexible with the arguments which it supports, translating to a large number of different formats supported syntactically. The general syntax for raise is:

							raise [Exception [, args [, traceback]]]

The first argument, Exception, is the name of the exception to raise. If present, it must either be a string, ...

Get Core Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.