What happens when I throw a C++ exception from a native Java method?(当我从本机 Java 方法抛出 C++ 异常时会发生什么?)
问题描述
假设我将 Sun 的 JVM 嵌入到 C++ 应用程序中.通过 JNI,我调用了一个 Java 方法(我自己的),该方法又调用了我在共享库中实现的本地方法.
Suppose I'm embedding Sun's JVM in a C++ application. Through JNI I call a Java method (my own), which in turns calls a native method I implemented in a shared library.
如果这个本地方法抛出 C++ 异常会发生什么?
What happens if this native method throws a C++ exception?
编译器是 gcc 3.4.x,jvm 是 sun 的 1.6.20.
edit: compiler is gcc 3.4.x, jvm is sun's 1.6.20.
推荐答案
在 JNI 文献中,exception 这个词似乎专门用于指代 Java 异常.本机代码中的意外事件称为编程错误.JNI 明确不要求 JVM 检查编程错误.如果发生编程错误,则行为未定义.不同的 JVM 可能表现不同.
Within the JNI literature, the word exception appears to be used exclusively to refer to Java exceptions. Unexpected events in native code are referred to as programming errors. JNI explicitly does not require JVMs to check for programming errors. If a programming error occurs, behavior is undefined. Different JVMs may behave differently.
将所有编程错误转换为返回码或 Java 异常是本机代码的职责.Java 异常不会立即从本机代码中抛出.它们可以是待处理,仅在本机代码返回给 Java 调用者时才抛出.本机代码可以使用 ExceptionOccurred
检查待处理的异常,并使用 ExceptionClear
清除它们.
It's the native code's responsibility to translate all programming errors into either return codes or Java exceptions. Java exceptions don't get thrown immediately from native code. They can be pending, only thrown once the native code returns to the Java caller. The native code can check for pending exceptions with ExceptionOccurred
and clear them with ExceptionClear
.
这篇关于当我从本机 Java 方法抛出 C++ 异常时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我从本机 Java 方法抛出 C++ 异常时会发生什么?


- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 转换 ldap 日期 2022-01-01