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

Get Practical Maya Programming with Python now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.