简体   繁体   English

如何通过SWIG到Java处理C ++中的异常

[英]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,异常和导演,但应该有帮助)让我给你一些建议:

  1. Don't send C++ exceptions up to the Java stack. 不要将C ++异常发送到Java堆栈。 It'll crash your application. 它会使你的应用程序崩溃。 Make sure they're all wrapped in the correct manner. 确保它们都以正确的方式包裹。 I know you're asking about this but it's really imperative you get that in. One missed exception can hose it all. 我知道你在问这个问题,但是你必须明白这一点。一个错过的例外就可以解决这个问题。
  2. Don't try passing Java exceptions down to the C++ stack, they get converted to SWIGDirectorExceptions or SWIGMethodExceptions. 不要尝试将Java异常传递给C ++堆栈,它们会转换为SWIGDirectorExceptions或SWIGMethodExceptions。 It's a real pain because you loose type information on the Java exception. 这是一个真正的痛苦,因为您丢失了Java异常的类型信息。 Instead, if you aren't dealing with a director, create a C++ class which does nothing more than raise C++ exceptions so that you can propogate C++ exceptions along the C++ stack. 相反,如果你不处理导演,那么创建一个C ++类,除了引发C ++异常之外什么也不做,这样你就可以在C ++栈中传播C ++异常。
  3. Wrap all naked strings passed from Java to C++ in a std::string. 在std :: string中包装从Java传递给C ++的所有裸字符串。 If you keep them as 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM