Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
Modeling a class in java 5 44
Saving Tweets with emojis using Twitter4J in a Mysql table 2 73
create a gui in perl 3 69
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

932 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