|
/********************************************************************************
File : jni_exception.cpp
Author:Jafar K (jafarmlp@googlemail.com)
Date/version : 02/02/07
********************************************************************************/
#include <jni_exception.h>
#include <org_tutorial_jni_util_exception_TestJniException.h>
JavaVM *cached_jvm; // A pointer to the VM from which
//we can get the JNIEnv for doing callbacks:
char g_azErrorMessage[ERROR_MESSAGE_LENGTH] = {0}; // Error message to Java
/******************************************************************************
JNI_OnLoad will be called when the jvm loads this library
******************************************************************************/
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
JNIEnv *env;
cached_jvm = jvm; /* cache the JavaVM pointer */
if ( jvm->GetEnv((void **)&env, JNI_VERSION_1_2)) {
printf("JNI version is not supported");
return JNI_ERR; /* JNI version not supported */
}
return JNI_VERSION_1_2;
}
/******************************************************************************
ASSERT example
******************************************************************************/
JNIEXPORT void JNICALL Java_org_tutorial_jni_util_exception_TestJniException_testexception
(JNIEnv *env, jobject object)
{
//Save the program state..
SAVE_PGM_STATE();
//
//...........
//
JNI_ASSERT(0,"This is a demo exception from c++ !");
//
//...........
//
}
/******************************************************************************
try/catch example
Param:
Return:
******************************************************************************/
JNIEXPORT void JNICALL Java_org_tutorial_jni_util_exception_TestJniException_function1
(JNIEnv *env, jobject object)
{
//
//...........
//
try
{
throw "this is a demo try catch exception ";
///Some more codes
}
catch (char *exception)
{
printf("An exception happened. Details: %s",exception);
}
try
{
int* myarray= new int[1000];
}
catch (bad_alloc&)
{
THROW_JAVA_EXCEPTION("Error allocating memory.\n");
}
//
//...........
//
}
/******************************************************************************
Restore the saved state.
Param :const char* pzFile,
int iLine,
const char* pzMessage
Return :
******************************************************************************/
void RestoreProgramState(const char* pzFile, int iLine,const char* pzMessage) {
//Copy the error message to the global array.
sprintf(g_azErrorMessage,"JNIException ! \n \
File \t\t: %s \n \
Line number \t\t: %d \n \
Reason for Exception\t: %s ",pzFile,iLine,pzMessage);
//Restore the saved/safe state.
RESTORE_SAFE_STATE();
}
/******************************************************************************
throws the exception to java
Param :const char* pzFile,
int iLine,
const char* pzMessage
Return :
******************************************************************************/
void ThrowJNIException(const char* pzFile, int iLine,const char* pzMessage)
{
//Check for null parameter
if(pzFile != NULL && pzMessage != NULL && iLine != 0)
sprintf(g_azErrorMessage,"JNIException ! \n \
File \t\t: %s \n \
Line number \t\t: %d \n \
Reason for Exception\t: %s ",pzFile,iLine,pzMessage);
jclass tClass = NULL;
//Findout the exception handling class
JNIEnv *env;
cached_jvm->AttachCurrentThread( (void **)&env, NULL );
if( env == NULL) {
printf("Invalid null pointer in ThrowJNIException " );
return;
}
tClass = env->FindClass(JNI_EXCEPTION_CLASS_NAME);
if (tClass == NULL) {
printf("Not found %s",JNI_EXCEPTION_CLASS_NAME);
return;
}
//Throw the excption with error info
env->ThrowNew(tClass,g_azErrorMessage );
env->DeleteLocalRef(tClass);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jafar is working in software field for the last five years. He is from Moonniyoor, a village of north Kerala,India.
Visit Jafar's home page http://jafarmlp.googlepages.com/