15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10.6. Raising Exceptions<br />

The interpreter was responsible for raising all of the exceptions we have seen so far. These exist as a<br />

result of encountering an error during execution. A programmer writing an API may also wish to throw<br />

an exception on erroneous input, for example, so <strong>Python</strong> provides a mechanism for the programmer to<br />

explicitly generate an exception: the raise statement.<br />

10.6.1. raise Statement<br />

Syntax and Common Usage<br />

The raise statement is quite flexible with the arguments it supports, translating to a large number of<br />

different formats supported syntactically. The general syntax for raise is:<br />

raise [SomeException [, args [, traceback]]]<br />

The first argument, SomeException, is the name of the exception to raise. If present, it must either be a<br />

string, class, or instance (more below). SomeException must be given if any of the other arguments (args<br />

or traceback) are present. A list of all <strong>Python</strong> standard exceptions is given in Table 10.2.<br />

The second expression contains optional args (aka parameters, values) for the exception. This value is<br />

either a single object or a tuple of objects. When exceptions are detected, the exception arguments are<br />

always returned as a tuple. If args is a tuple, then that tuple represents the same set of exception<br />

arguments that are given to the handler. If args is a single object, then the tuple will consist solely of<br />

this one object (i.e., a tuple with one element). In most cases, the single argument consists of a string<br />

indicating the cause of the error. When a tuple is given, it usually equates to an error string, an error<br />

number, and perhaps an error location, such as a file, etc.<br />

The final argument, TRaceback, is also optional (and rarely used in practice), and, if present, is the<br />

traceback object used for the exceptionnormally a traceback object is newly created when an exception<br />

is raised. This third argument is useful if you want to reraise an exception (perhaps to point to the<br />

previous location from the current). Arguments that are absent are represented by the value None.<br />

The most common syntax used is when SomeException is a class. No additional parameters are ever<br />

required, but in this case, if they are given, they can be a single object argument, a tuple of arguments,<br />

or an exception class instance. If the argument is an instance, then it can be an instance of the given<br />

class or a derived class (subclassed from a pre-existing exception class). No additional arguments (i.e.,<br />

exception arguments) are permitted if the argument is an instance.<br />

More Exotic/Less Common Usage<br />

What happens if the argument is an instance? No problems arise if instance is an instance of the given<br />

exception class. However, if instance is not an instance of the class or an instance of a subclass of the<br />

class, then a new instance of the exception class will be created with exception arguments copied from<br />

the given instance. If instanceis an instance of a subclass of the exception class, then the new exception<br />

will be instantiated from the subclass, not the original exception class.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!