Throw my own Exception (from JNI code)

From CodeCodex

Implementations[edit]

Java[edit]

The MyOwnException class

public class MyOwnException extends Exception {
  public MyOwnException(){}
  public MyOwnException(String txt) {
    super(txt);
    }
}

the prototype in the header file

JNIEXPORT void JNICALL Java_JavaHowTo_triggerException
  (JNIEnv *, jobject);

the JNI function

JNIEXPORT void JNICALL Java_JavaHowTo_triggerException
  (JNIEnv *env, jobject obj) {
    jclass MyOwnException = env->FindClass("MyOwnException");
    env->ThrowNew(MyOwnException,"Exception triggered from JNI routine.");
}

The Java JNI wrapper would be

class JavaHowTo {
  public native void triggerException();
  static {
    System.loadLibrary("javahowto"); 
  }
}

And finally, to use it

public class JNIJavaHowTo {
  public static void main(String[] args) {
    JavaHowTo jht = new JavaHowTo();
    jht.triggerException();
    }
}