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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.