throw null investigation

B

bob smith

Is it common practice to throw a null exception when writing JNI code?

I'm seeing an exception that is simply a null rather than an object, and I'm wondering if it's because it's easier to do that in JNI.
 
D

Daniel Pitts

Is it common practice to throw a null exception when writing JNI code?

I'm seeing an exception that is simply a null rather than an object, and I'm wondering if it's because it's easier to do that in JNI.
From what I can tell, that shouldn't ever happen. I'm not sure in JNI,
but I know in Java if you throw a "null", it will instead throw a
NullPointerException, so anyone catching that Throwable will not see a
"null" value, but instead a NullPointerException instance.
 
M

markspace

From what I can tell, that shouldn't ever happen. I'm not sure in JNI,
but I know in Java if you throw a "null", it will instead throw a
NullPointerException, so anyone catching that Throwable will not see a
"null" value, but instead a NullPointerException instance.


In Java, yes. But in JNI, in C, I'd expect it is possible to literally
throw an empty or null pointer.

I'd also expect that any code that did that was sub-par, and not worth
using or debugging.

I'm mildly interested in what the OP is using, mostly so I can avoid it.
However if he can supply some details (*what* are you using, Bob?) I'd
consider taking a loot at it. The actually question he asks: "Is it
common practice," I don't really know or care. It seems bad practice,
but I've never dealt with JNI code, so what do I know?
 
D

Daniele Futtorovic

In Java, yes. But in JNI, in C, I'd expect it is possible to literally
throw an empty or null pointer.

I'd also expect that any code that did that was sub-par, and not worth
using or debugging.

I'm mildly interested in what the OP is using, mostly so I can avoid it.
However if he can supply some details (*what* are you using, Bob?) I'd
consider taking a loot at it. The actually question he asks: "Is it
common practice," I don't really know or care. It seems bad practice,
but I've never dealt with JNI code, so what do I know?

But if the JNI code actually wanted to throw an NPE, and teh Jav wraps a
null into an NPE, wouldn't it actually be more efficient if the JNI code
"threw" a null?
 
M

markspace

But if the JNI code actually wanted to throw an NPE, and teh Jav wraps a
null into an NPE, wouldn't it actually be more efficient if the JNI code
"threw" a null?


That's what I'm saying. I think perhaps Java does NOT wrap a null
exception thrown by JNI code. Java wraps a null value when you throw it
from Java, but JNI tends to have a lot of holes, especially if you are
dealing with "signed" or "trusted" code. There's ways to get around the
checks Java does of JNI code in the interest of speed, and if you don't
check, then you don't get protection.
 
D

Daniele Futtorovic

That's what I'm saying. I think perhaps Java does NOT wrap a null
exception thrown by JNI code. Java wraps a null value when you throw it
from Java, but JNI tends to have a lot of holes, especially if you are
dealing with "signed" or "trusted" code. There's ways to get around the
checks Java does of JNI code in the interest of speed, and if you don't
check, then you don't get protection.

But if what you say is correct, and Java does /not/ systematically wrap
nulls "thrown" from JNI (assuming it's possible), then it would be
technically possible to catch a null Exception in the Java runtime,
wouldn't it?
 
M

markspace

But if what you say is correct, and Java does /not/ systematically wrap
nulls "thrown" from JNI (assuming it's possible), then it would be
technically possible to catch a null Exception in the Java runtime,
wouldn't it?


I want to emphasize there than I'm speculating and extrapolating from
barely remembered blogs. It's something for the OP to consider, but
he's going to have to decide if I'm talking rubbish or not.

Executive summary: I dunno.
 
D

Daniel Pitts

I want to emphasize there than I'm speculating and extrapolating from
barely remembered blogs. It's something for the OP to consider, but
he's going to have to decide if I'm talking rubbish or not.

Executive summary: I dunno.
Lets check the JLS...
 
D

Daniel Pitts

Lets check the JLS...
Actually, the JLS doesn't seem to handle this, but the JNI spec does:

The JNI does not check for programming errors such as passing in NULL pointers or illegal argument types. [snip]
The programmer must not pass illegal pointers or arguments of the wrong type to JNI functions. Doing so could result in arbitrary consequences, including a corrupted system state or VM crash.

It seems to me then, if the JNI code is passing NULL to "Throw" or
"ThrowNew", it is "undefined" behavior.
 
R

Roedy Green

Is it common practice to throw a null exception when writing JNI code?

It is an unusual thing to do. It has the same effect of throwing a
NullPointerException. Sun had to figure out what to do if the
reference thrown were null. That seemed as reasonable a way of
handling it as any. However, in my view, it is really a mistake. You
should not be writing code that looks like a mistake.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the exponential function.
~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top