O'Reilly logo

Practical Maya Programming with Python by Robert Galanakis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required