Ctypes 捕获异常
Ctypes catching exception
我正在玩ctypes和C/C++ DLL我有一个非常简单的"数学"dll
double Divide(double a, double b)
{
if (b == 0)
{
throw new invalid_argument("b cannot be zero!");
}
return a / b;
}
到目前为止它有效唯一的问题是,我在 Python 中收到 WindowsError 异常,并且无法检索文本b 不能为零我必须抛出一些特殊的异常类型吗?还是必须更改 Python 代码?蟒蛇代码:
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"
浏览 Python 2.7.3 的 ctypes 文档,我看不到任何对C++的引用,也看不到通过 ctypes 调用C++代码中抛出异常。似乎 ctypes 仅用于调用 C 函数,不处理C++异常。
试试这个:
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"
相关文章:
- 当类定义不可见时捕获异常
- 来自 Android 应用程序内部的 boost 类型的 boost::wrapexcept<boost::system::system_error> 的未捕获异常
- 如何通过 pybind11 从 python 中的C++中捕获异常?
- 信号后未捕获异常
- 捕获异常后如何退出程序执行
- C++ 捕获异常后进行清理的标准方法是什么?
- 使用模板类引发和捕获异常
- E/libc++abi:终止于类型为google::protobuf::FatalException的未捕获异常
- 如果在生成 std::thread 后引发,则未捕获异常
- C++ 未捕获异常,程序将终止并中止
- C++程序在第一次尝试时会给出垃圾,但如果它捕获异常并重试,则会给出适当的值
- 仅捕获异常就可以检测所有二进制文件在C 中读取错误是否足够
- 如何捕获 I/O 异常(确切地说是 I/O,而不是 std::exception)
- 为什么捕获异常播放允许尾括号
- throw() 函数应该总是在异常时展开堆栈并允许捕获异常还是必须调用 std::terminate ?
- 寻求与类型为 std::invalid_argument 的未捕获异常相关的运行时错误的建议: stoi:无转换
- 如何从调用函数中捕获异常
- 当用户在字符数组中输入整数值时捕获异常
- C++按值捕获异常时的示例是不好的
- Ctypes 捕获异常