gpt4 book ai didi

java - 使用 JNI 的 Java 代码流

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:50:34 25 4
gpt4 key购买 nike

我有 JNI 级别的代码。它可以抛出异常。代码是:

#include "JavaGlueClass.h"
#include <stdio.h>
#include <windows.h>
#include <string.h>

jint throwNoClassDefError(JNIEnv *env, const char *message)
{
jclass exClass;
char *className = (char *) "java/lang/NoClassDefFoundError" ;

exClass = env->FindClass(className);

if ( exClass != NULL ) {
return env->ThrowNew(exClass, message);
}

//free the local ref
env->DeleteLocalRef(exClass);

}

void throwGetFieldIDException(JNIEnv *env)
{
const char *className = "GetFieldIDException";
jclass exClass = env->FindClass(className);
if (exClass == NULL) {
throwNoClassDefError(env, className);
} else {
env->ThrowNew(exClass, "GetFieldIDException message");
}
env->DeleteLocalRef(exClass);
printf("printprint");
}




JNIEXPORT jobject JNICALL Java_JavaGlueClass_test(JNIEnv *env, jobject obj)
{
jmethodID constructor;
jobject object;
jclass clazz;
jfieldID fid;
jstring stringField;

clazz = env->FindClass("Information");
if (clazz == 0) {
printf("error while finding class");
throwNoClassDefError(env, "no such class");
} else {
//create object throuht constructor
constructor = env->GetMethodID(clazz, "<init>", "()V");
object = env->NewObject(clazz, constructor);

// set private value1 field
stringField = env->NewStringUTF("str1");
//fid = env->GetFieldID(clazz,"value1","Ljava/lang/String;");
fid = NULL;
if (fid == NULL) {
throwGetFieldIDException(env, "error with value1 field.");
//return NULL;
}

env->SetObjectField(object, fid, stringField);

//set private value2 field
fid = env->GetFieldID(clazz,"value2","I");
if (fid == NULL) {
throwGetFieldIDException(env, "error with value1 field.");
//return NULL;
}
env->SetIntField(object, fid, 1);

// set private value3 field
stringField = env->NewStringUTF("str2");
fid = env->GetFieldID(clazz,"value3","Ljava/lang/String;");
if (fid == NULL) {
throwGetFieldIDException(env, "error with value1 field.");
//return NULL;
}
env->SetObjectField(object, fid, stringField);

//set private value4 field
fid = env->GetFieldID(clazz,"value4","I");
if (fid == NULL) {
throwGetFieldIDException(env, "error with value1 field.");
//return NULL;
}
env->SetIntField(object, fid, 2);

printf("end of cpp function");
return object;
}

}

Java_JavaGlueClass_test 函数创建自定义信息类的对象。每次我设置类字段时,都会检查“fid”是否为 NULL。我读到有关 JNI 异常的信息,这些异常是挂起的异常。我发现将抛出的第一个异常将代码流移动到 Java 级别(而不是 JNI 级别)。但是在 JNI 级别异常之后的其余代码呢?据我了解,它将被执行。是否有可能在第一个异常之后抛出第二个、第三个等异常?我是否应该在抛出异常后返回 NULL 或其他东西,以便 JNI 级别的其余代码不会被执行?

最佳答案

来自 the JNI specification :

A pending exception raised through the JNI (by calling ThrowNew, for example) does not immediately disrupt the native method execution. This is different from how exceptions behave in the Java programming language. When an exception is thrown in the Java programming language, the virtual machine automatically transfers the control flow to the nearest enclosing try/catch statement that matches the exception type. The virtual machine then clears the pending exception and executes the exception handler. In contrast, JNI programmers must explicitly implement the control flow after an exception has occurred.

关于java - 使用 JNI 的 Java 代码流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13845430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com