Catching python unhandled exception

I’m batch processing files with a pipeline of scripts.
Every now and then there is a unhandled exception and I get a dialog something like this:
image
@stevebaer I think you are the one to ask if there are means to handle this without a full stop.

Ideally I’d have an option to set what to do in case of such an event.

In my case, I would like to collect the Traceback, write it to a log file, Save the file and shut Rhino down then. Best would be to proceed to the next file for processing but by means of starting a New Rhino instance that can pickup where it stranded.

In short a custom handling of unhandled exceptions.

Thanks,

-Willem

Wrap everything in a try except block using the logging module to log the exception message?

Hi @Willem, you can catch zero division errors individually, along with other errors:

def DoSomething():
    a = 1
    b = 0
    try:
        c = a / b
    except ZeroDivisionError:
        print "Error: You attempted to divide by zero"
        return
    except Exception as ex:
        print "Error: {}".format(ex)
    finally:
        print "Info: This always is done, regardless of errors"
DoSomething()

_
c.

Hi Clement,

This is not specific to zero divisions, but about general unandled errors.
It can be anything like Rhino not creating guid’s from bad geometry, not returning lists where expected.

Technically it’s a result of bad coding but practically it’s impossible to catch everything.
Especially with arbitrary input geometry and code that is not yet adapted to this automated purpose.

-Willem

Hi Garham,

Thanks!
That is worth a try, I’m using a wrapper script anyway so I could add it there.

-Willem

Hi @Willem, that’s why i included a second except in above example. You can track any error with this or specific ones with the first except. The finally statement allows to put results into a log, regardless of the case (error or not).

You might also access a traceback object with detailed information in case of an exception using sys.exc_info()

_
c.

1 Like