15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

the exception handler. Although reasons for exceptions are optional, the standard built-in exceptions do<br />

provide at least one argument, an error string indicating the cause of the exception.<br />

Exception parameters can be ignored in the handler, but the <strong>Python</strong> provides syntax for saving this<br />

value. We have already seen it in the syntax above: to access any provided exception reason, you must<br />

reserve a variable to hold the argument. This argument is given on the except header line and follows<br />

the exception type you are handling. The different syntaxes for the except statement can be extended to<br />

the following:<br />

# single exception<br />

except Exception[, reason]:<br />

suite_for_Exception_with_Argument<br />

# multiple exceptions<br />

except (Exception1, Exception2, ..., ExceptionN)[, reason]:<br />

suite_for_Exception1_to_ExceptionN_with_Argument<br />

reason is a class instance containing diagnostic information from the code raising the exception. The<br />

exception arguments themselves go into a tuple that is stored as an attribute of the class instance, an<br />

instance of the exception class from which it was instantiated. In the first alternate syntax above, reason<br />

is an instance of the Exception class.<br />

For most standard built-in exceptions, that is, exceptions derived from StandardError, the tuple consists<br />

of a single string indicating the cause of the error. The actual exception name serves as a satisfactory<br />

clue, but the error string enhances the meaning even more. Operating system or other environment<br />

type errors, i.e., IOError, will also include an operating system error number that precedes the error<br />

string in the tuple.<br />

Whether a reason contains just a string or a combination of an error number and a string, calling str<br />

(reason) should present a human-readable cause of an error. However, do not lose sight that reason is<br />

really a class instanceyou are only getting the error information via that class's special method __str__<br />

(). We have a complete treatment of special methods as we explore object-oriented programming in<br />

Chapter 13.<br />

The only caveat is that not all exceptions raised in third-party or otherwise external modules adhere to<br />

this standard protocol of error string or error number and error string. We recommend you follow such a<br />

standard when raising your own exceptions (see <strong>Core</strong> Style note).<br />

<strong>Core</strong> Style: Follow exception argument protocol<br />

When you raise built-in exceptions in your own code, try to follow the<br />

protocol established by the existing <strong>Python</strong> code as far as the error<br />

information that is part of the tuple passed as the exception<br />

argument. In other words, if you raise a ValueError, provide the same<br />

argument information as when the interpreter raises a ValueError<br />

exception, and so on. This helps keep the code consistent and will<br />

prevent other applications that use your module from breaking.<br />

The example below is when an invalid object is passed to the float() built-in function, resulting in a<br />

TypeError exception:

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

Saved successfully!

Ooh no, something went wrong!