[4382] in java-interest

home help back first fref pref prev next nref lref last post

Re: Exceptions from java native methods!!

daemon@ATHENA.MIT.EDU (Ken Arnold - Sun Labs)
Thu Dec 21 14:06:03 1995

Date: Thu, 21 Dec 1995 12:11:37 -0500
From: arnold@suneast.East.Sun.COM (Ken Arnold - Sun Labs)
To: java-interest@webrunner.neato.org

>Can native methods in Java throw exceptions?  PlainSocketImpl class
>defines private native void socketCreate(boolean stream) throws
>IOException; I am very surprised to see this, because code for the
>native methods are in C Dlls.  First there are no exceptions in C, how
>does this work?  Can any one shed some light on this?
>
>Thanks in advance
>Ram rgolla@novell.com

Yes, you can use the SignalError() function to tell the runtime
to raise a particular exception after your native method returns.
It would look something like this:

	MyClass_nativeMethod(...) {
	    if (badArguments(...)) {
		SignalError(NULL, "java/io/IllegalArgumentException", NULL);
		return;
	    }
	    /* ... */
	}

The first parameter is the execution environment, where NULL means
"the current one" (almost always what you want).  The second is the name
of the exception class, with any "." changed to "/".  The third is the
descriptive string parameter for the exception, if any.  If you provide
one, the exception type should support a single-string constructor.

		Ken

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post