Book Image

Practical Maya Programming with Python

By : Robert Galanakis
Book Image

Practical Maya Programming with Python

By: Robert Galanakis

Overview of this book

Table of Contents (17 chapters)
Practical Maya Programming with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Understanding exceptions


The word exception is loaded. The definition seems clear: exceptions are exceptional. I'll say, in Python at least, this definition is simply not true. A normal Python program may handle and raise any number of exceptions as it hums along quite nicely.

Consider the Pythonic idiom we have already pointed out multiple times: Easier to Ask Forgiveness than Permission. It is usually expressed as follows:

try:
    spam.eggs()
except AttributeError:
    spam.ham()

The preceding code is preferred over the following code, which uses a Look Before You Leap style:

if hasattr(spam, 'eggs'):
    spam.eggs()
else:
    spam.ham()

There is nothing exceptional about exceptions in the first example. Describing them as exceptional is more accurate when describing how they behave rather than how they are used.

I prefer the following definition:

"Exceptions are a form of (exceptional) flow control."

To illustrate that definition, consider the following lines of code:

if spam:
    for i in xrange...