Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-08-23
3
Medium Priority
?
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

715 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