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

Building a high-level error handler


I stated in a previous section that it's desirable to know every time a script errors. There are two ways to do this. The first is to embed some notification behavior in the tool itself using something like the following code:

def my_tool_main():
    try:
        do_stuff()
    except Exception:
        send_error_mail()
        raise

This is a fine strategy for larger programs. But what happens if you want to do this for hundreds of scripts serving dozens of artists? Embedding the error handler in each script would lead to rampant duplication and boilerplate.

Python has a solution, of course. It is sys.excepthook (also called the exception hook), described by the Python docs as follows:

"When an exception is raised and uncaught, the interpreter calls sys.excepthook with three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program...