Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Native Code Throwing Developer-Defined Java Exceptions

Thread view: 
res7cxbi@verizon.net - 01 Jan 2006 03:34 GMT
How do you throw exceptions i defined myself in java from native code
(C++)?
Gordon Beaton - 01 Jan 2006 13:37 GMT
> 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

Signature

[  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

res7cxbi@verizon.net - 01 Jan 2006 20:57 GMT
Thanks this is just what i needed
res7cxbi@verizon.net - 02 Jan 2006 00:14 GMT
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?
res7cxbi@verizon.net - 02 Jan 2006 00:28 GMT
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);
}
Gordon Beaton - 02 Jan 2006 08:20 GMT
> And here's the revised C++ code:
> //ExceptionExperimentImpl.cpp
[quoted text clipped - 7 lines]
>           env->DeleteLocalRef(cls);
> }

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

/gordon

Signature

[  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

res7cxbi@verizon.net - 03 Jan 2006 01:17 GMT
Really? The "A Utility Function" (6.1.2) at the URL
http://java.sun.com/docs/books/jni/html/exceptions.html#11202 does this
Gordon Beaton - 03 Jan 2006 11:55 GMT
> 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

Signature

[  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

Roedy Green - 03 Jan 2006 03:27 GMT
>Actually, now i have another problem:

I would recommend getting a textbook with some examples.

See http://mindprod.com/jgloss/jni.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.