[JNI] Problem throwing my own exceptions

L

Loic Minier

Hi,

[ This is not a repost, but has already been asked on java.lang.help. ]

I am trying to throw exceptions of the class RasException, which is
defined in my default package. I do also define other classes in the
same default package.
The general goal of the classes is to wrap the
RAS API to access functions like RasEnumEntries
(<http://msdn.microsoft.com/library/en-us/rras/rras/rasenumentries.asp>
) in Java. So I've got a PhoneBook Java class which has a native
method:
public native RasEntryName[] EnumEntries() throws RasException;

The native method instanciate a jarray and the jobjects of class
RasEntryName. Please note that this works without problem!

I am also perfectly capable of throwing java.lang.* Throwable like
java.lang.Exception or java.lang.InternalError.

However, trying to FindClass my RasException results in a
ClassDefNotFound.

The CPP macros used look like this:

// makes a "new x(y)" Exception object and throws it
#define THROW_CUSTOM_EXCEPTION_MESSAGE(x, y) \
{ \
jclass jclass_x = env->FindClass((#x)); \
if (jclass_x == 0) { \
env->ExceptionClear(); \
jclass_x = env->FindClass("java/lang/Exception"); \
if (jclass_x == 0) { \
DEFAULT_RETURN \
} \
} \
jstring jstring_s = env->NewStringUTF((#y)); \
/* search the constructor */ \
jmethodID jmethodid_constructor = \
env->GetMethodID(jclass_x, "<init>", "(Ljava/lang/String;)V" ); \
jobject jobject_o = \
env->NewObject(jclass_x, jmethodid_constructor, jstring_s); \
env->Throw((jthrowable)jobject_o); \
DEFAULT_RETURN \
}
// construct an throw an exception with class x and message y
#define THROW_EXCEPTION_MESSAGE(x, y) \
{ \
jclass jclass_x = env->FindClass((#x)); \
if (jclass_x != 0) { /* we were able to find the exception class */ \
env->ThrowNew(jclass_x, (#y)); \
} \
DEFAULT_RETURN \
}
#define THROW_INTERNAL_ERROR(x) \
THROW_EXCEPTION_MESSAGE("java/lang/InternalError", (#x))
#define THROW_OUT_OF_MEMORY_ERROR \
THROW_EXCEPTION_MESSAGE("java/lang/OutOfMemoryError", \
"thrown from RasWrapper.dll")
#define THROW_RAS_EXCEPTION(x) \
THROW_CUSTOM_EXCEPTION_MESSAGE("RasException", \
"Unhandled RasException thrown from RasWrapper.dll")

You might wonder how RasException looks like, if it references other
classes etc. So here with the very basic code of RasException:

public class RasException extends Exception {
public RasException() {
super();
}

public RasException(String message) {
super(message);
}

public RasException(Throwable cause) {
super(cause);
}

public RasException(String message, Throwable cause) {
super(message, cause);
}
}

I wrote a test program wich lists the entries of the system phonebook,
a custom phonebook, no phonebook at all etc.
The problem arise on the fourth run, where a RasException is supposed
to be thrown, but is not because FindClass throws. So I instanciate an
Exception and get:

java.lang.Exception: "Unhandled RasException thrown from RasWrapper.dll"
at PhoneBook.EnumEntries(Native Method)
at RasTest.main(RasTest.java:11)
Exception in thread "main"

Which is of course to be expected.

A complete log of my test program is at:
<http://via.ecp.fr/~lool/java/rasexception.log>


Any hint on the above problem?
 
L

Loïc Minier

[SNIP apparently JNI problem which wasn't one]

In case anyone was interested, the problem was not caused by JNI at all,
but by the CPP macros themselves. I can not understand why right now, but I
think I'll "throw my compiler thru the Windows".


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top