Catch all unhandled exceptions in MaxScript?




I’m hijacking pythons sys.excepthook, in order to catch all unhandled exceptions (send error e-mails).

It works well in MotionBuilder and to a degree in Max, but only if the caller is Python (say a button in a qt window). If the caller is maxscript, it seems the exception is caught and forwarded to maxscript, thus never reaching my exception handler.

I’d like to somehow catch the exception before or after it goes to maxscript, if that’s at all a posibility. Alternatively I could alter any existing code that calls python from maxscript (using a combination of Python.Execute throwOnError: False and python.GetLastError() ), but this would mean knowing about high level exception handeling scattered around out code, which smells a bit…

If someone has solved this in an elegant way, I’d love to know!




Here is what I’ve done at Volition. We have a MaxScript library, v_logger which is globally accessible. v_logger contains functions to log info, warning, and error level events. For us those functions call out to our Python based error logging system. They could just as easily directly emit emails, save to a db, or write to a log file.

Every MaxScript in our tools is wrapped in this outer try/catch:

try (
   -- code goes here
) catch (
   stack to:v_logger.callstack -- property on the v_logger struct
   v_logger.log_error <tool_name> <extra_information>

v_logger will include the callstack in the information it emits.


Cool Jeff, that’s very useful. I’m guessing you don’t get the full Python traceback in case of a python exception (rather the Max Digest version)?

My ideal solution would still be to employ something at a system level, so the rest of my code only needs to concern itself with specific exceptions it want’s to catch :slight_smile:


No Python stuff, no. For the few places where we execute MaxScript from external Python tools we ensure we’re set to catch unhandled exceptions on the Python side. We’ve not created any tools in Max’s internal Python API yet.


This now works great in Max. Every call to a Python function from MaxScript (from an ActionItem or callback) is wrapped in try/except on the python side. On exception our reporting handler is called explicitly and the exception re-raised.

Unfortunately MotionBuilders Python exception handeling, is not propperly initialized before the Python Editor is opened (which no artists do), so I’m a but stuck there :slight_smile:

Thanks for the pointers.


Is there no problems by wrapping all your maxscript with catch and try ? Like performance problems