Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm playing a little bit with ctypes and C/C++ DLLs I have a quite 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 cannot retrieve the text b cannot be zero Is there some special exception type I must throw? Or must the Python code be altered? 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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...