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.

try:<br />

... float(['float() does not', 'like lists', 2])<br />

... except TypeError, diag:# capture diagnostic info<br />

... pass<br />

...<br />

>>> type(diag)<br />

<br />

>>><br />

>>> print diag<br />

float() argument must be a string or a number<br />

The first thing we did was cause an exception to be raised from within the try statement. Then we<br />

passed cleanly through by ignoring but saving the error information. Calling the type() built-in function,<br />

we were able to confirm that our exception was indeed an instance of the TypeError exception class.<br />

Finally, we displayed the error by calling print with our diagnostic exception argument.<br />

To obtain more information regarding the exception, we can use the special __class__ instance attribute,<br />

which identifies which class an instance was instantiated from. Class objects also have attributes, such<br />

as a documentation string and a string name that further illuminate the error type:<br />

>>> diag # exception instance object<br />

<br />

>>> diag.__class__ # exception class object<br />

<br />

>>> diag.__class__.__doc__ # exception class documentation string<br />

'Inappropriate argument type.'<br />

>>> diag.__class__.__name__ # exception class name<br />

'TypeError'<br />

As we will discover in Chapter 13Classes and OOP the special instance attribute __class__ exists for all<br />

class instances, and the __doc__ class attribute is available for all classes that define their documentation<br />

strings.<br />

We will now update our safe_float() one more time to include the exception argument, which is passed<br />

from the interpreter from within float()when exceptions are generated. In our last modification to<br />

safe_float(), we merged both the handlers for the ValueError and TypeError exceptions into one<br />

because we had to satisfy some requirement. The problem, if any, with this solution is that no clue is<br />

given as to which exception was raised or what caused the error. The only thing returned is an error<br />

string that indicated some form of invalid argument. Now that we have the exception argument, this no<br />

longer has to be the case.<br />

Because each exception will generate its own exception argument, if we chose to return this string<br />

rather than a generic one we made up, it would provide a better clue as to the source of the problem. In<br />

the following code snippet, we replace our single error string with the string representation of the<br />

exception argument.<br />

def safe_float(object):<br />

try:<br />

retval = float(object)<br />

except (ValueError, TypeError), diag:<br />

retval = str(diag)

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

Saved successfully!

Ooh no, something went wrong!