Solved

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

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 90
login form jsp example 2 73
Which non-HTML GUI front end to use with Java? 3 67
Running JavaFX on the Raspberry Pi 27 299
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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

752 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