I am fairly new to JNI goodness so please just tell me if I am being stupid.
I am trying to throw a custom exception from C++ to the Java layer that gets constructed with both a string and an integer. I can't get ThrowNew() to work because It only takes one string parameter. EVERY example of a custom exception that I could find uses ThrowNew()!! Grrrrrr
I am guessing that I will need to costruct one with and throw it with "Throw(jthrowable obj)" but I am not sure.. is that the same as "ThrowNew()"??
For example, this is what I need:
int myErrorCode = 42;
const char* myErrorString = "stuff broke";
jclass myExceptionClass = env->FindClass("MyException");
env->ThrowNew(myExceptionClass, myErrorString, myErrorCode );//<-- of course this wont work!
This code works fine if my exception class looks like Exception() and takes only a String.
Does anyone know how to construct and throw a NEW instance of an exception to java that takes parameters other than Exception()'s default string? Throwing only a string is worthless to me.
Thanks in advance!
You can use env->Throw
to throw an instance you create manually:
jclass myExceptionClass = env->FindClass("MyException");
jstring myErrorJString = env->NewStringUTF(myErrorString);
jmethodID ctorMethod = env->GetMethodID(myExceptionClass, "<init>", "(Ljava/lang/String;I)V");
jobject myExceptionObject = env->NewObject(myExceptionClass, ctorMethod, myErrorJString, myErrorCode);
env->Throw(myExceptionObject);