我通过JNI从Java类调用DLL,我想捕获Java类中的异常并相应地处理由DLL引发的异常。能否帮我举例说明如何处理这些。
提前致谢!!
答案 0 :(得分:0)
在适当的情况下,通过ThrowNew
抛出相应的Java异常类或您自己的构造之一。
http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#wp5234
{
......
//Somewhere in my JNI/native code, its time to throw an exception
//Pass my JNIEnv variable & my message
throwBasicException( env, "Oh Nos, I should put my details here" );
.....
}
void throwBasicException( JNIEnv * env, std::string message )
{
jclass exceptionClass = env->FindClass(env, "java/lang/Exception");
jstring javaMessage = env->NewStringUTF( message.c_str() );
env->ThrowNew(env, exceptionClass, javaMessage);
env->DeleteLocalRef(javaMessagE);
}