如何处理来自 C++ 的异常通过 SWIG 转 Java

发布于 2024-09-25 00:44:45 字数 207 浏览 4 评论 0原文

我们正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-10-02 00:44:45

另请参阅 Swig 2.0 文档中的有关异常处理的 Java 特定部分

为了避免多次编写该模式,我创建了一个 SWIG 宏,支持抛出一种类型的 C++ 异常的方法 - 通过捕获该异常并抛出相应的 Java 异常:

WRAP_THROW_EXCEPTION( myCppDeclaration, com::foo::MyCppExceptionClass, 
  "com.foo.MyException",
  "com/foo/MyException" );

这是宏:

%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

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:

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
記憶穿過時間隧道 2024-10-02 00:44:45

由于我一直在努力解决这个问题(请参阅我的个人资料中的博客,它是关于 python、SWIG、异常和控制器的内容,但应该会有所帮助),让我给您一些建议:

  1. 不要将 C++ 异常发送到 Java 堆栈。它会让你的应用程序崩溃。确保它们都以正确的方式包裹。我知道您在问这个问题,但您确实有必要了解这一点。一个错过的异常就可以解决所有问题。
  2. 不要尝试将 Java 异常传递到 C++ 堆栈,它们会转换为 SWIGDirectorExceptions 或 SWIGMethodExceptions。这确实很痛苦,因为您丢失了 Java 异常的类型信息。相反,如果您不与主管打交道,请创建一个 C++ 类,它只会引发 C++ 异常,以便您可以沿着 C++ 堆栈传播 C++ 异常。
  3. 将从 Java 传递到 C++ 的所有裸字符串包装在 std::string 中。如果将它们保留为 const char 指针,Java 将可以选择对它们进行垃圾收集。所有项目都是如此,但它是一个很容易被忽视的项目,我已经做过并且已经看到它做过几次了。

之后就不要再读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:

  1. Don't send C++ exceptions up to the Java stack. 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. It's a real pain because you loose type information on the Java exception. 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.
  3. Wrap all naked strings passed from Java to C++ in a std::string. If you keep them as 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文