Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > throw null investigation

Reply
Thread Tools

throw null investigation

 
 
bob smith
Guest
Posts: n/a
 
      07-18-2012
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.
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      07-18-2012
On 7/18/12 10:10 AM, bob smith wrote:
> 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.

 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      07-18-2012
On 7/18/2012 12:39 PM, Daniel Pitts wrote:
> On 7/18/12 10:10 AM, bob smith wrote:
>> 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.
>



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?


 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      07-18-2012
On 18/07/2012 22:35, markspace allegedly wrote:
> On 7/18/2012 12:39 PM, Daniel Pitts wrote:
>> On 7/18/12 10:10 AM, bob smith wrote:
>>> 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.
>>

>
>
> 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?

--
DF.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      07-18-2012
On 7/18/2012 2:33 PM, Daniele Futtorovic wrote:

> 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.



 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      07-18-2012
On 18/07/2012 23:43, markspace allegedly wrote:
> On 7/18/2012 2:33 PM, Daniele Futtorovic wrote:
>
>> 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.


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?

--
DF.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      07-18-2012
On 7/18/2012 3:06 PM, Daniele Futtorovic wrote:
>
> 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.



 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      07-18-2012
On 7/18/12 3:55 PM, markspace wrote:
> On 7/18/2012 3:06 PM, Daniele Futtorovic wrote:
>>
>> 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.
>
>
>

Lets check the JLS...

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      07-18-2012
On 7/18/12 4:00 PM, Daniel Pitts wrote:
> On 7/18/12 3:55 PM, markspace wrote:
>> On 7/18/2012 3:06 PM, Daniele Futtorovic wrote:
>>>
>>> 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.
>>
>>
>>

> Lets check the JLS...
>

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

<http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp17593>
> 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.

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-19-2012
On Wed, 18 Jul 2012 10:10:02 -0700 (PDT), bob smith
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>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)
http://www.youtube.com/watch?v=F-QA2rkpBSY


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To throw or to throw not? Emanuele D'Arrigo Python 6 11-15-2008 04:12 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
JNI's throw new does not throw an exception yarona@m-sys.com Java 15 09-08-2005 08:36 AM
Throw Exception Vs Throw New Exception Kerri ASP .Net 2 10-27-2003 02:13 PM
Investigation techniques FreeChemistry Java 2 07-18-2003 03:37 PM



Advertisments