异常时会发生什么

异常时会发生什么

本文介绍了当我从本机Java方法抛出C ++异常时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我将Sun的JVM嵌入到C ++应用程序中。通过JNI我调用了一个Java方法(我自己的),它依次调用我在一个共享库中实现的本地方法。



如果此本机方法引发C ++异常,会发生什么?



编辑器是gcc 3.4.x, jvm是sun的1.6.20。

解决方案

在JNI文献中,单词异常似乎是专用于引用Java异常。本地代码中的意外事件被称为编程错误。 JNI显然不需要JVM来检查编程错误。如果发生编程错误,行为是未定义的。不同的JVM可能会有所不同。



本机代码有责任将所有编程错误转换为返回码或Java异常。 Java异常不会立即从本机代码抛出。它们可以是等待,只有当本地代码返回到Java调用者时才抛出。本地代码可以使用 ExceptionOccurred 检查挂起的异常,并使用 ExceptionClear 清除它们。


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.

What happens if this native method throws a C++ exception?

edit: compiler is gcc 3.4.x, jvm is sun's 1.6.20.

解决方案

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.

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 ++异常时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:53