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/l ang/String ;)Ljava/la ng/String;
*/
JNIEXPORT jstring JNICALL Java_TestException_getFile Type
(JNIEnv *, jobject, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
JNIEXPORT jstring JNICALL Java_TestException_getFile Type
(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
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/l
*/
JNIEXPORT jstring JNICALL Java_TestException_getFile
(JNIEnv *, jobject, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
JNIEXPORT jstring JNICALL Java_TestException_getFile
(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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oooooooooopss!!!
forgot to reload the question
forgot to reload the question
Thank you for accepting, glad I was of help :)
ASKER
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,"jav a/lang/Exc eption");
if (excCls != 0)
{
(*env)->ThrowNew(env, excCls, getError());
return ""; //ok ->Is this must here??
//return ""; //not ok
}
}
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,"jav
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.
ASKER
ok, I understand that
Thanks for nice links :-)
Thanks for nice links :-)
:)
ASKER