Ctypes exception exception

I play a little with ctypes and C / C ++ DLL. I have a pretty simple math DLL

double Divide(double a, double b)
{
    if (b == 0)
    {
       throw new invalid_argument("b cannot be zero!");
    }

    return a / b;
}

It works so far the only problem, I get a WindowsError exception in Python, and I can't get the text b cannot be null Is there any special type of exception that I have to throw? Or should Python code be modified? python code:

from ctypes import *

mathdll=cdll.MathFuncsDll
divide = mathdll.Divide
divide.restype = c_double
divide.argtypes = [c_double, c_double]

try:
    print divide (10,0)
except WindowsError:
    print "lalal"
except:
    print "dada"
+3
source share
2 answers

First of all, you should not use C ++ exception specifications. This is a terrible feature of C ++, and it is deprecated in the latest standard. In addition, the syntax is throw(...)invalid C ++ in general, this line does not compile with a standard corresponding compiler like gcc:

double Divide(double a, double b) throw (...)

, Visual ++ "", , , , Visual ++, , throw() .

ctypes Python 2.7.3 ++ ++, ctypes. , ctypes C ++.

+1

:

from ctypes import *

mathdll=cdll.MathFuncsDll
divide = mathdll.Divide
divide.restype = c_double
divide.argtypes = [c_double, c_double]

try:
    print divide (10,0)
except WindowsError as we:
    print we.args[0]
except:
    print "Unhandled Exception"
0

All Articles