[英]How to handle exceptions from C++ via SWIG to Java
We are implementing a wrapper on C++ code for exposure to Java clients. 我们正在实现一个C ++代码包装器,以便接触Java客户端。 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)?
我已经看过关于异常处理的SWIG文档,但这在三层(C ++ / SWIG / Java)中的编码术语中有什么意义呢?
If anybody has working example(s) or advice, I would be grateful. 如果有人有工作实例或建议,我将不胜感激。
See also in the Swig 2.0 documentation this Java-specific section on exception handling . 另请参阅Swig 2.0文档中有关异常处理的特定于Java的部分 。
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: 为了避免多次编写模式,我创建了一个SWIG宏,支持抛出一种类型的C ++异常的方法 - 通过捕获它并抛出相应的Java异常:
WRAP_THROW_EXCEPTION( myCppDeclaration, com::foo::MyCppExceptionClass,
"com.foo.MyException",
"com/foo/MyException" );
Here's the macro: 这是宏:
%define WRAP_THROW_EXCEPTION( MATCH, CPPTYPE, JTYPE, JNITYPE )
%javaexception(JTYPE) MATCH {
try {
$action
}
catch ( CPPTYPE & e ) {
jclass eclass = jenv->FindClass(JNITYPE);
if ( eclass ) {
jenv->ThrowNew( eclass, e.what() );
}
}
}
%enddef
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: 因为我已经和我搏斗了(从我的个人资料中查看我的博客,它是关于python,SWIG,异常和导演,但应该有帮助)让我给你一些建议:
const char
pointers Java will have the option to garbage collect them. const char
指针,Java将具有垃圾收集它们的选项。 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. 之后,请不要阅读1.1。 Use the documentation from 2.0 or 1.3.
使用2.0或1.3中的文档。 It's much more clear.
它更清楚了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.