Native Code Throwing Developer-Defined Java Exceptions

Discussion in 'Java' started by res7cxbi@verizon.net, Jan 1, 2006.

  1. Guest

    How do you throw exceptions i defined myself in java from native code
    (C++)?
     
    , Jan 1, 2006
    #1
    1. Advertising

  2. On 31 Dec 2005 19:34:45 -0800, wrote:
    > How do you throw exceptions i defined myself in java from native
    > code (C++)?


    Have you looked at the JNI specification? There is a section
    describing functions for handling exceptions.

    Use Throw() or ThrowNew().

    /gordon


    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 1, 2006
    #2
    1. Advertising

  3. Guest

    Thanks this is just what i needed
     
    , Jan 1, 2006
    #3
  4. Guest

    Actually, now i have another problem:

    Here's my Java Code:
    //ExceptionExperiment.java
    public class ExceptionExperiment {

    public native void testException() throws MyException;

    static {
    System.loadLibrary("ExceptionExperimentImpl");
    }

    public ExceptionExperiment() {
    }

    public static void main(String[] args) {
    ExceptionExperiment app = new ExceptionExperiment();
    try {
    app.testException();
    System.out.print("a");
    }catch(MyException e) {
    e.printStackTrace();
    }
    }

    public class MyException extends Exception {
    public MyException() {
    super();
    }

    public MyException(String s) {
    super(s);
    }
    }
    }

    And here's the C++ code:
    //ExceptionExperimentImpl.cpp
    JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
    (JNIEnv *env, jobject obj)
    {
    jclass cls =
    env->FindClass("ExceptionExperiment$MyException");
    if (cls != NULL) {
    env->ThrowNew(cls, NULL);
    }
    env->DeleteLocalRef(cls);
    }

    Now, this doesn't throw the MyException, but throws this:
    //output
    java.lang.NoSuchMethodError: ExceptionExperiment$MyException: method
    <init>()V not found
    at ExceptionExperiment.testException(Native Method)
    at ExceptionExperiment.main(ExceptionExperiment.java:15)

    >From this output it tells me that it can find the class but can't find

    the <init>()V method (which is the constructor, right?).
    Am i doing something wrong here?
     
    , Jan 2, 2006
    #4
  5. Guest

    Problem Solved!

    I made the MyException class a separate class rather than an inner
    class of ExceptionExperiment

    The revised java code:
    //ExceptionExperiment.java
    public class ExceptionExperiment {


    public native void testException() throws MyException;


    static {
    System.loadLibrary("ExceptionExperimentImpl");
    }


    public ExceptionExperiment() {
    }


    public static void main(String[] args) {
    ExceptionExperiment app = new ExceptionExperiment();
    try {
    app.testException();
    System.out.print("a");
    }catch(MyException e) {
    e.printStackTrace();
    }
    }
    }
    class MyException extends Exception {
    public MyException() {
    super();
    }

    public MyException(String s) {
    super(s);
    }
    }

    And here's the revised C++ code:
    //ExceptionExperimentImpl.cpp
    JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
    (JNIEnv *env, jobject obj)
    {
    jclass cls = env->FindClass("MyException");
    if (cls != NULL) {
    env->ThrowNew(cls, NULL);
    }
    env->DeleteLocalRef(cls);
    }
     
    , Jan 2, 2006
    #5
  6. On 1 Jan 2006 16:28:38 -0800, wrote:
    > And here's the revised C++ code:
    > //ExceptionExperimentImpl.cpp
    > JNIEXPORT void JNICALL Java_ExceptionExperiment_testException
    > (JNIEnv *env, jobject obj)
    > {
    > jclass cls = env->FindClass("MyException");
    > if (cls != NULL) {
    > env->ThrowNew(cls, NULL);
    > }
    > env->DeleteLocalRef(cls);
    > }


    Note that it is rarely necessary to need DeleteLocalRef(). All local
    references will be "deleted" when the native method returns.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 2, 2006
    #6
  7. Guest

    , Jan 3, 2006
    #7
  8. Roedy Green Guest

    On 1 Jan 2006 16:14:09 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >Actually, now i have another problem:


    I would recommend getting a textbook with some examples.

    See http://mindprod.com/jgloss/jni.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 3, 2006
    #8
  9. On 2 Jan 2006 17:17:48 -0800, wrote:
    > Really? The "A Utility Function" (6.1.2) at the URL
    > http://java.sun.com/docs/books/jni/html/exceptions.html#11202 does this


    Yes it does. Not because it's technically necessary to do so, but
    rather to follow a guideline (described in section 5.3 rules for
    managing references) that says a utility function should avoid
    accumulating local references.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 3, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anusrik
    Replies:
    4
    Views:
    942
    Chris Uppal
    May 9, 2006
  2. Joe
    Replies:
    0
    Views:
    590
  3. Bill Medland

    Problems throwing from C Native Code

    Bill Medland, Jun 26, 2006, in forum: Java
    Replies:
    1
    Views:
    391
    Bill Medland
    Jun 26, 2006
  4. Oodini
    Replies:
    1
    Views:
    1,842
    Keith Thompson
    Sep 27, 2005
  5. Isaac
    Replies:
    0
    Views:
    404
    Isaac
    Jan 20, 2011
Loading...

Share This Page