type lookuperror

Chris Angelico rosuav at gmail.com
Wed Aug 17 22:22:31 EDT 2016


On Thu, Aug 18, 2016 at 12:13 PM, meInvent bbird <jobmattcon at gmail.com> wrote:
> would like to check errors for every function i run,
> got error type lookuperror
>
> def checkexception(func, **kwargs):
>     try:
>         result = func(*tuple(value for _, value in kwargs.iteritems()))
>     except:
>         print "Unexpected error:", sys.exc_info()[0]
>         try:
>             print(func.__doc__)
>         except:
>             print("no doc error")
>

I'm going to be brutally honest, and simply say that this is terrible
code. I'm not even going to _try_ to fix it. Instead, here's a
completely rewritten form:

def checkexception(func, *args, **kwargs):
    try:
        result = func(*args, **kwargs)
    except BaseException as e:
        print("Exception raised: %s" % e)
        try: print(func.__doc__)
        except AttributeError: pass
        raise # Let the exception keep happening.

But really, there are even better ways to do this. Just let the
exception happen, and then use something like ipython to help you
analyze the traceback.

ChrisA



More information about the Python-list mailing list