我正在使用以下代碼嘗試在java中獲取類RWException(它擴展了Exception),以便我可以調用方法「getCode()」來檢索一個錯誤代碼(int)並正確處理錯誤。我通過JNI文檔並創建了以下代碼...問題是,當我試圖調用無參數方法getCode()時,我得到了AccessViolation異常。我得到了一個有效的類和我正在尋找的方法ID的句柄。JNI獲取異常信息 - 嘗試讀取或寫入受保護的內存
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);
}
此代碼給了我一個異常而調試inVS.NET /以下信息寬:
試圖讀取或寫入保護內存
UPDATE 這裏是Java方法祝通過上面的JNI代碼調用:
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()方法嘗試並捕獲該異常。然後,需要通過getCode()方法從RWException對象獲取代碼 - 在java中它會沿着int代碼行= oRWexception.getCode(); – bbqchickenrobot
是的,發生異常是因爲它沒有正確捕獲 –