Native Code Throwing Developer-Defined Java Exceptions

G

Gordon Beaton

How do you throw exceptions i defined myself in java from native
code (C++)?

Have you looked at the JNI specification? There is a section
describing functions for handling exceptions.

Use Throw() or ThrowNew().

/gordon
 
R

res7cxbi

Actually, now i have another problem:

Here's my Java Code:
//ExceptionExperiment.java
public class ExceptionExperiment {

public native void testException() throws MyException;

static {
System.loadLibrary("ExceptionExperimentImpl");
}

public ExceptionExperiment() {
}

public static void main(String[] args) {
ExceptionExperiment app = new ExceptionExperiment();
try {
app.testException();
System.out.print("a");
}catch(MyException e) {
e.printStackTrace();
}
}

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

public MyException(String s) {
super(s);
}
}
}

And here's the C++ code:
//ExceptionExperimentImpl.cpp
JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
(JNIEnv *env, jobject obj)
{
jclass cls =
env->FindClass("ExceptionExperiment$MyException");
if (cls != NULL) {
env->ThrowNew(cls, NULL);
}
env->DeleteLocalRef(cls);
}

Now, this doesn't throw the MyException, but throws this:
//output
java.lang.NoSuchMethodError: ExceptionExperiment$MyException: method
<init>()V not found
at ExceptionExperiment.testException(Native Method)
at ExceptionExperiment.main(ExceptionExperiment.java:15)
From this output it tells me that it can find the class but can't find
the <init>()V method (which is the constructor, right?).
Am i doing something wrong here?
 
R

res7cxbi

Problem Solved!

I made the MyException class a separate class rather than an inner
class of ExceptionExperiment

The revised java code:
//ExceptionExperiment.java
public class ExceptionExperiment {


public native void testException() throws MyException;


static {
System.loadLibrary("ExceptionExperimentImpl");
}


public ExceptionExperiment() {
}


public static void main(String[] args) {
ExceptionExperiment app = new ExceptionExperiment();
try {
app.testException();
System.out.print("a");
}catch(MyException e) {
e.printStackTrace();
}
}
}
class MyException extends Exception {
public MyException() {
super();
}

public MyException(String s) {
super(s);
}
}

And here's the revised C++ code:
//ExceptionExperimentImpl.cpp
JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
(JNIEnv *env, jobject obj)
{
jclass cls = env->FindClass("MyException");
if (cls != NULL) {
env->ThrowNew(cls, NULL);
}
env->DeleteLocalRef(cls);
}
 
G

Gordon Beaton

And here's the revised C++ code:
//ExceptionExperimentImpl.cpp
JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
(JNIEnv *env, jobject obj)
{
jclass cls = env->FindClass("MyException");
if (cls != NULL) {
env->ThrowNew(cls, NULL);
}
env->DeleteLocalRef(cls);
}

Note that it is rarely necessary to need DeleteLocalRef(). All local
references will be "deleted" when the native method returns.

/gordon
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top