Solved

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

Posted on 2001-08-23
3
285 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now