如何处理来自 C++ 的异常通过 SWIG 转 Java
我们正在 C++ 代码上实现一个包装器,以便向 Java 客户端公开。我看过 有关异常处理的 SWIG 文档,但这是什么意思翻译成三层(C++/SWIG/Java)的编码术语?
如果有人有可行的例子或建议,我将不胜感激。
We are implementing a wrapper on C++ code for exposure to Java clients. I have seen the SWIG documents about exception handling but what does this translate to in coding terms in the three layers (C++/SWIG/Java)?
If anybody has working example(s) or advice, I would be grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另请参阅 Swig 2.0 文档中的有关异常处理的 Java 特定部分 。
为了避免多次编写该模式,我创建了一个 SWIG 宏,支持抛出一种类型的 C++ 异常的方法 - 通过捕获该异常并抛出相应的 Java 异常:
这是宏:
See also in the Swig 2.0 documentation this Java-specific section on exception handling.
To avoid writing the pattern more than once, I created a SWIG macro supporting methods that throw one type of C++ exception -- by catching that and throwing a corresponding Java exception:
Here's the macro:
由于我一直在努力解决这个问题(请参阅我的个人资料中的博客,它是关于 python、SWIG、异常和控制器的内容,但应该会有所帮助),让我给您一些建议:
之后就不要再读1.1了。使用 2.0 或 1.3 中的文档。这更清楚了。
Since I've wrestled with this (see my blog from my profile, it's on python, SWIG, exceptions and directors but should help) let me give you a few pieces of advice:
const char
pointers Java will have the option to garbage collect them. This is true of all items but it's such an easily overlooked one that I've done it and seen it done a few times already.After that, don't read 1.1. Use the documentation from 2.0 or 1.3. It's much more clear.