Link to home
Start Free TrialLog in
Avatar of sudhakar_koundinya
sudhakar_koundinya

asked on

How to throw Exceptions in JNI

Hello all,

How to throw Exceptions in JNI??

class  TestException
{
      public native String getFileType(String magicFile,String strFile)throws Exception;
      public static void main(String[] args)
      {
            System.out.println("Hello World!");
      }
}


javah generated code

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestException */

#ifndef _Included_TestException
#define _Included_TestException
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     TestException
 * Method:    getFileType
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_TestException_getFileType
  (JNIEnv *, jobject, jstring, jstring);

#ifdef __cplusplus
}
#endif
#endif


JNIEXPORT jstring JNICALL Java_TestException_getFileType
  (JNIEnv *env, jobject object , jstring str1, jstring str2)
{
//some blah blah blah
 
 //now assume exception raised here. then how should c source should return exception object to JVM.
}

Thanks and Best Regards
Sudhakar
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

ASKER

thanks. take ur A
oooooooooopss!!!
forgot to reload the question
Thank you for accepting, glad I was of help :)
girionis

one quick question

after throwing error if i don't return anything JVM is raising exception. Is it must to return some thing
if(strlen(getError())>0)
      {
            jclass excCls = (*env)->FindClass(env,"java/lang/Exception");
            if (excCls != 0)
            {
                  (*env)->ThrowNew(env, excCls, getError());
                  return ""; //ok ->Is this must here??
                  //return ""; //not ok
            }
      }

Are you calling it from a C function that returns something? If yes then I'd expect that you need to return something anyway.
ok, I understand that

Thanks for nice links :-)