Solved

how to catach a exception object in native function (JNI)?

Posted on 2001-08-23
3
287 Views
Last Modified: 2013-11-23
I know we can catch java exception from native language by using JNI methods: <p>

ExceptionOccurred()<P>
ExceptionDescribe()<P>
ExceptionClear()<P>

but how about if I want to catch a particular exception object ? For example, I've the following derived Java exception class

class MyException extends Exception
{
   private int code;
   public MyException(){};
   public MyException(String msg)
   {
       super(msg);
   }

   public MyException(String msg, int c)
   {
      super(msg);
      code = c;
    }

    public int GetCode()
    {
        return code;
    }

    public void SetCode(int c)
    {
     code = c;
    }
}

how to I catch a MyException in the native function ?
0
Comment
Question by:eugeneng
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6419947
In a nutshell, you have to call the ExceptionOccurred function to determine whether the Java method threw an exception. This function returns an object of type Throwable. If it returns NULL, then there was no exception thrown. When used in conjunction with the FindClass and IsInstanceOf functions, you can determine which exception was thrown and behave appropriately. Example:

jclass myExcClass = env->FindClass("com/project/MyException");
jclass nullExcClass = env->FindClass("java/lang/NullPointerException");
jthrowable thrownObj;

if ((thrownObj = env->ExceptionOccurred()) != NULL) {
    if (env->IsInstanceOf(thrownObj, nullExcClass) == JNI_TRUE) {
        // handle NullPointerException
        env->ExceptionClear();
    } else if (env->IsInstanceOf(thrownObj, myExcClass) == JNI_TRUE) {
        // handle MyException
        env->ExceptionClear();
    } else {
        // don't know this exception
        return;
    }
}

When you've handled an exception you need to call ExceptionClear so that it doesn't propogate back to the Java code that called the native method.

The Java Tutorial contains a short description of JNI exception handling
    http://java.sun.com/docs/books/tutorial/native1.1/implementing/error.html

And here's a link to the section of the JNI Spec that discusses exception handling
    http://java.sun.com/j2se/1.3.0/docs/guide/jni/spec/design.doc.html#770

Best regards,
Jim Cakalic
0
 

Author Comment

by:eugeneng
ID: 6425853
hi jim, fisrt of all, that for you answer, you solved half of my problem which is to determine what kind of exception has been thrown. The another question is how do I access the member function of the exception object ? I haven't try out yet, but I this since IsExceptionOccurred() will return the exception object, so I should be able to access the exception's member function such as following:

jclass myExcClass = env->FindClass("com/project/MyException");
jclass nullExcClass = env->FindClass("java/lang/NullPointerException");
jthrowable thrownObj;

thrownObj = env->ExceptionOccurred();

if ((thrownObj  != NULL)
{
   if (env->IsInstanceOf(thrownObj, nullExcClass) == JNI_TRUE)
    {
       // handle NullPointerException
       env->ExceptionClear();
   }
   else
    if (env->IsInstanceOf(thrownObj, myExcClass) == JNI_TRUE)
    {
       jmethodID mid = env->GetMethodID(myExcClass,"GetInt","()I");
       jint intRes;
       intRes = CallObjectMethod((jobject)jhrownObj,mid);
       env->ExceptionClear();
   } else {
       // don't know this exception
       return;
   }
}


will the above code works, especially the calling exception method GetInt() part?
0
 

Author Comment

by:eugeneng
ID: 6427379
I've tried the above example, it doesn't works!!! but it works if I change to as following :

jclass myExcClass = env->FindClass("com/project/MyException");
jclass nullExcClass = env->FindClass("java/lang/NullPointerException");
jthrowable thrownObj;

thrownObj = env->ExceptionOccurred();

if ((thrownObj  != NULL)
{
  if (env->IsInstanceOf(thrownObj, nullExcClass) == JNI_TRUE)
   {
      // handle NullPointerException
      env->ExceptionClear();
  }
  else
   if (env->IsInstanceOf(thrownObj, myExcClass) == JNI_TRUE)
   {
      env->ExceptionClear();
      jmethodID mid = env->GetMethodID(myExcClass,"GetInt","()I");
      jint intRes;
      intRes = CallIntMethod(jhrownObj,mid);
  } else {
      // don't know this exception
      return;
  }
}

apparently, I should use CallIntMethod and clear the exception before I call this function.

anyway, the problem is solved, thanx alot jim

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 108
egit plugin on eclipse 8 63
Maven Project: Hibernate Dependencies Conflict 10 23
hibernate example for saving data 19 42
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question