Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C++

Exception handling in JNI

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
7 Feb 20074 min read 77.9K   440   17  
An article on Exception handling in JNI
/********************************************************************************
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
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/

Comments and Discussions