What happens when I throw a C++ exception from a native Java method?

前端 未结 5 1694
时光取名叫无心
时光取名叫无心 2020-12-08 07:22

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.

5条回答
  •  我在风中等你
    2020-12-08 08:12

    I am guessing your JVM will crash. Native C++ exceptions do not propagate into Java through JNI. One reason for that is that JNI is a C interface, and C knows nothing of C++ exceptions.

    What you have to do is catch the C++ exceptions before you get into the C layer of your JNI code, and make the JNI C function return an error code. Then you can check for the error code inside Java and throw a Java exception if necessary.

提交回复
热议问题