Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #15995 > unrolled thread

e==null? really?

Started bybob smith <bob@coolfone.comze.com>
First post2012-07-13 07:18 -0700
Last post2012-07-14 06:30 -0700
Articles 12 — 9 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  e==null?  really? bob smith <bob@coolfone.comze.com> - 2012-07-13 07:18 -0700
    Re: e==null?  really? markspace <-@.> - 2012-07-13 07:29 -0700
      Re: e==null?  really? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-13 10:48 -0400
        Re: e==null?  really? markspace <-@.> - 2012-07-13 08:22 -0700
          Re: e==null?  really? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-07-13 09:35 -0700
        Re: e==null?  really? Lew <lewbloch@gmail.com> - 2012-07-13 13:11 -0700
          Re: e==null?  really? Joerg Meier <joergmmeier@arcor.de> - 2012-07-13 22:55 +0200
            Re: e==null?  really? "javax.swing.JSnarker" <gharriman@boojum.mit.edu> - 2012-07-13 17:01 -0400
      Re: e==null?  really? Lew <lewbloch@gmail.com> - 2012-07-13 13:09 -0700
    Re: e==null?  really? Roedy Green <see_website@mindprod.com.invalid> - 2012-07-13 12:09 -0700
      Re: e==null?  really? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-07-13 23:06 +0200
        Re: e==null?  really? Roedy Green <see_website@mindprod.com.invalid> - 2012-07-14 06:30 -0700

#15995 — e==null? really?

Frombob smith <bob@coolfone.comze.com>
Date2012-07-13 07:18 -0700
Subjecte==null? really?
Message-ID<c42a91b5-4e73-4647-8dc8-58b258243c2a@googlegroups.com>
Is it possible to do this in Java?

throw null;


What probably happened if I am looking at code like this:

	} catch (Exception e) {

and e is null?

I'm seeing this, and I just don't get it.

[toc] | [next] | [standalone]


#15996

Frommarkspace <-@.>
Date2012-07-13 07:29 -0700
Message-ID<jtpbc8$23s$1@dont-email.me>
In reply to#15995
On 7/13/2012 7:18 AM, bob smith wrote:
> Is it possible to do this in Java?
>
> throw null;


Yes. JLS:

<http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.18>


"throw Expression ;

The Expression in a throw statement must denote either 1) a variable or 
value of a reference type which is assignable (§5.2) to the type 
Throwable, or 2) the null reference..."

[toc] | [prev] | [next] | [standalone]


#15997

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-07-13 10:48 -0400
Message-ID<jtpcfj$99l$1@dont-email.me>
In reply to#15996
On 7/13/2012 10:29 AM, markspace wrote:
> On 7/13/2012 7:18 AM, bob smith wrote:
>> Is it possible to do this in Java?
>>
>> throw null;
>
>
> Yes. JLS:
>
> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.18>
>
>
> "throw Expression ;
>
> The Expression in a throw statement must denote either 1) a variable or
> value of a reference type which is assignable (§5.2) to the type
> Throwable, or 2) the null reference..."

     Interesting. Reading onward, though, we find

	"If evaluation of the Expression completes normally,
	producing a null value, then an instance V' of class
	NullPointerException is created and thrown instead of
	null. [...]"

... which leaves a bit of a mystery.  The O.P.'s example was
(fleshed out somewhat)

	try {
	    throw null;
	} catch (Exception e) {
	    // e is null here!
	}

... but according to the JLS that shouldn't have happened.  Bug?

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#15998

Frommarkspace <-@.>
Date2012-07-13 08:22 -0700
Message-ID<jtpegl$m3a$1@dont-email.me>
In reply to#15997
On 7/13/2012 7:48 AM, Eric Sosman wrote:

>      Interesting. Reading onward, though, we find


Thanks for pointing that out.  I'd guess there's no bug, just the OP 
having a hard time interpreting his results.

[toc] | [prev] | [next] | [standalone]


#16000

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-07-13 09:35 -0700
Message-ID<j7YLr.2653$jA7.1906@newsfe15.iad>
In reply to#15998
On 7/13/12 8:22 AM, markspace wrote:
> On 7/13/2012 7:48 AM, Eric Sosman wrote:
>
>>      Interesting. Reading onward, though, we find
>
>
> Thanks for pointing that out.  I'd guess there's no bug, just the OP
> having a hard time interpreting his results.

Indeed.
class NullExceptionSSCCE {
    public static void main(String[] str) {
     try {
       throw null;
     } catch (Exception e) {
       System.out.println("e = " + e);
     }
   }
}

=====
e = java.lang.NullPointerException
=====


If the OP was correct, it would have printed "e = null"

Without a real SSCCE from the OP, it's hard to say in what way the 
results were misinterpreted.

[toc] | [prev] | [next] | [standalone]


#16007

FromLew <lewbloch@gmail.com>
Date2012-07-13 13:11 -0700
Message-ID<57cde03f-92eb-48bf-9da8-ced150c0f921@googlegroups.com>
In reply to#15997
Eric Sosman wrote:
> markspace wrote:
> &gt; bob smith wrote:
> &gt;&gt; Is it possible to do this in Java?
> &gt;&gt;
> &gt;&gt; throw null;
> &gt;
> &gt;
> &gt; Yes. JLS:
> &gt;
> &gt; &lt;http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.18&gt;
> &gt;
> &gt;
> &gt; &quot;throw Expression ;
> &gt;
> &gt; The Expression in a throw statement must denote either 1) a variable or
> &gt; value of a reference type which is assignable (§5.2) to the type
> &gt; Throwable, or 2) the null reference...&quot;
> 
>      Interesting. Reading onward, though, we find
> 
> 	&quot;If evaluation of the Expression completes normally,
> 	producing a null value, then an instance V&#39; of class
> 	NullPointerException is created and thrown instead of
> 	null. [...]&quot;
> 
> ... which leaves a bit of a mystery.  The O.P.&#39;s example was
> (fleshed out somewhat)

No mystery. The first refers to a compiler rule, the second to a runtime rule.

> 	try {
> 	    throw null;
> 	} catch (Exception e) {
> 	    // e is null here!
> 	}
> 
> ... but according to the JLS that shouldn&#39;t have happened.  Bug?

It's no more a bug than that you might get a 'ClassCastException' when you cast
a value to another type. The compiler allows what the runtime sometimes 
doesn't.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#16008

FromJoerg Meier <joergmmeier@arcor.de>
Date2012-07-13 22:55 +0200
Message-ID<u0u3deibmi0q$.1aiqvfhru9gi6$.dlg@40tude.net>
In reply to#16007
On Fri, 13 Jul 2012 13:11:18 -0700 (PDT), Lew wrote:

>> 	try {
>> 	    throw null;
>> 	} catch (Exception e) {
>> 	    // e is null here!
>> 	}

>> ... but according to the JLS that shouldn&#39;t have happened.  Bug?
> It's no more a bug than that you might get a 'ClassCastException' when you cast
> a value to another type. The compiler allows what the runtime sometimes 
> doesn't.

And there's really no way around that - Exception e = null; if
(externalCondition()) e = new Exception(); throw e; - can't very well
complain about that at compile time for arbitrary definitions of
externalCondition().

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#16009

From"javax.swing.JSnarker" <gharriman@boojum.mit.edu>
Date2012-07-13 17:01 -0400
Message-ID<jtq2br$2uj$1@speranza.aioe.org>
In reply to#16008
On 13/07/2012 4:55 PM, Joerg Meier wrote:
> On Fri, 13 Jul 2012 13:11:18 -0700 (PDT), Lew wrote:
>
>>> 	try {
>>> 	    throw null;
>>> 	} catch (Exception e) {
>>> 	    // e is null here!
>>> 	}
>
>>> ... but according to the JLS that shouldn&#39;t have happened.  Bug?
>> It's no more a bug than that you might get a 'ClassCastException' when you cast
>> a value to another type. The compiler allows what the runtime sometimes
>> doesn't.
>
> And there's really no way around that - Exception e = null; if
> (externalCondition()) e = new Exception(); throw e; - can't very well
> complain about that at compile time for arbitrary definitions of
> externalCondition().

Well, there is one; "throw e;" *could* have been specified to compile 
into what you presently get by compiling:

if (e == null)
     throw new NullPointerException();
else
     throw e;

-- 
public final class JSnarker
extends JComponent
A JSnarker is an NNTP-aware component that asynchronously provides 
snarky output when the Ego.needsPuncturing() event is fired in cljp.

[toc] | [prev] | [next] | [standalone]


#16006

FromLew <lewbloch@gmail.com>
Date2012-07-13 13:09 -0700
Message-ID<9b23ffd0-48d7-4c0a-8500-54345ae5fb4c@googlegroups.com>
In reply to#15996
markspace wrote:
> bob smith wrote:
> &gt; Is it possible to do this in Java?
> &gt;
> &gt; throw null;
> 
> 
> Yes. JLS:

Good place to get the final word, albeit with a bit of effort occasionally.

> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.18>
> 
> 
> "throw Expression ;
> 
> The Expression in a throw statement must denote either 1) a variable or 
> value of a reference type which is assignable (§5.2) to the type 
> Throwable, or 2) the null reference..."

Know. Don't guess. Read the JLS.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#16003

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-07-13 12:09 -0700
Message-ID<3es0085pttibvtod0lbnhtn2kccch9v0jk@4ax.com>
In reply to#15995
On Fri, 13 Jul 2012 07:18:37 -0700 (PDT), bob smith
<bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone
who said :

>	} catch (Exception e) {
>
>and e is null?

Most likely you are misinterpreting the results. Try tracing the
program and put a breakpoint inside your catch.  
-- 
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

[toc] | [prev] | [next] | [standalone]


#16010

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-07-13 23:06 +0200
Message-ID<jtq2lg$msj$1@dont-email.me>
In reply to#16003
On 13/07/2012 21:09, Roedy Green allegedly wrote:
> On Fri, 13 Jul 2012 07:18:37 -0700 (PDT), bob smith
> <bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone
> who said :
> 
>> 	} catch (Exception e) {
>>
>> and e is null?
> 
> Most likely you are misinterpreting the results. Try tracing the
> program and put a breakpoint inside your catch.  

I'd rather say: if there's the slightest whiff of fishiness with what
your debugger tells you, throw it to the four winds and use logging instead.

In my observation debuggers are almost as often a source of confusion as
they are one of clarification. YMMV.

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#16016

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-07-14 06:30 -0700
Message-ID<h2t2081o6gogtjtnmhe78au052pdi3cm5v@4ax.com>
In reply to#16010
On Fri, 13 Jul 2012 23:06:36 +0200, Daniele Futtorovic
<da.futt.news@laposte-dot-net.invalid> wrote, quoted or indirectly
quoted someone who said :

>In my observation debuggers are almost as often a source of confusion as
>they are one of clarification. YMMV.

the main thing is to look from a slightly different angle.
-- 
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web