【发布时间】:2012-09-07 21:51:20
【问题描述】:
我正在使用以下代码尝试在 java 中获取一个类 RWException(它扩展了 Exception),以便我可以调用方法“getCode()”来检索错误代码 (int) 并正确处理错误。我浏览了 JNI 文档并创建了以下代码...问题是当我尝试调用无参数方法 getCode() 时遇到 AccessViolation 异常。我得到了类的有效句柄和我正在寻找的方法 id。
jstring o = (jstring)envLoc->CallStaticObjectMethod(cls, mid, jstrUser, jstrPass, jstrGroup);
jthrowable exc = envLoc->ExceptionOccurred();
if (exc) {
// Get the class
jclass mvclass = env->GetObjectClass( exc );
// Get method ID for method
jmethodID mid = env->GetMethodID(mvclass, "getCode", "()I");
// Call the method
jint code = env->CallIntMethod(mvclass, mid);
}
这段代码在使用以下信息在 VS.NET 中调试时给了我一个异常:
试图读取或写入受保护的内存
更新 这是我希望通过上面的 JNI 代码调用的 java 方法:
public int getCode() {
return code;
}
mvclass 和 mid 对象都已正确实例化,并且应该可以正常工作,除非我遗漏了什么。
更新 2
如果我运行以下代码,则 toString() 方法使用相同的概念:
jstring o = (jstring)envLoc->CallStaticObjectMethod(cls, mid, jstrUser, jstrPass, jstrGroup);
exc = envLoc->ExceptionOccurred();
if (exc) {
envLoc->ExceptionClear();
// Get the class
jclass exccls = envLoc->GetObjectClass(exc);
// Get method ID for methods
jmethodID getCodeMeth = envLoc->GetMethodID(exccls, "getCode", "()I");
jmethodID getMsgMeth = envLoc->GetMethodID(exccls, "toString", "()Ljava/lang/String;");
jstring obj = (jstring)envLoc->CallObjectMethod(exccls, getMsgMeth);
String^ toString = JStringToCliString(obj);
// this is where the access violation occurs
jint jcode = envLoc->CallIntMethod(exccls, getCodeMeth);
int code = jcode;
}
所以,toString() 方法返回对象的完整类名,它是正确的 RWException 对象。第一次更新 getCode() 中概述的方法是公开的,等等......所以不知道为什么它会给出内存访问冲突错误。
【问题讨论】:
-
异常很容易尝试捕获并完成。
-
java 端发生异常...它抛出 RWException 类型的异常,然后我尝试通过 ExceptionOccurred() 方法捕获该异常。然后我需要通过 RWException 对象的代码getCode() 方法 - 在 java 中,它将沿着 int code = oRWexception.getCode(); 的 hte 行
-
是的,发生异常是因为没有正确捕获
-
@RomanC 通常需要传播异常而不是在内部捕获它。如果每个异常都在抛出它的块内被捕获,那么整个异常点都会丢失。
-
@RomanC 由编写代码的人决定。 OP 正在捕捉异常 - 他的问题正是关于处理它。他只是碰巧用本机方法来做。
标签: java c++ java-native-interface