How to catch a usefull error message ?

Vincent Vande Vyvre vincent.vande.vyvre at
Tue Apr 23 05:56:08 EDT 2019


In a CPython lib I have an _init() method wich take one argument, a file 

     char *fname;

     if (!PyArg_ParseTuple(args, "s", &fname))
         return NULL;

So, if I instanciate my object with a bad argument I've a good error 

tif = ImgProc(123)
TypeError: argument 1 must be str, not int
(followed by the traceback)

But if I do:
     tif = ImgProc(123)
except Exception as why:
     print("Error:", why)

I get just:

Error: <class '_liboqapy.ImgProc'> returned a result with an error set

Without traceback. That's not very usefull.

I prefer to keep the instanciation of this object into a try-except bloc 
but how to read the error message ?


More information about the Python-list mailing list