当我从本机 Java 方法抛出 C++ 异常时会发生什么?

What happens when I throw a C++ exception from a native Java method?(当我从本机 Java 方法抛出 C++ 异常时会发生什么?)

本文介绍了当我从本机 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++ 异常时会发生什么?

基础教程推荐