Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > e==null? really?

Reply
Thread Tools

e==null? really?

 
 
bob smith
Guest
Posts: n/a
 
      07-13-2012
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.
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      07-13-2012
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..."


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-13-2012
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
d


 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      07-13-2012
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.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      07-13-2012
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-13-2012
On Fri, 13 Jul 2012 07:18:37 -0700 (PDT), bob smith
<> 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


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-13-2012
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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-13-2012
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-14.18&gt;
> &gt;
> &gt;
> &gt; &quot;throw Expression ;
> &gt;
> &gt; The Expression in a throw statement must denote either 1) a variableor
> &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' of class
> NullPointerException is created and thrown instead of
> null. [...]&quot;
>
> ... which leaves a bit of a mystery. The O.P.'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'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
 
Reply With Quote
 
Joerg Meier
Guest
Posts: n/a
 
      07-13-2012
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'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.
 
Reply With Quote
 
javax.swing.JSnarker
Guest
Posts: n/a
 
      07-13-2012
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'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.


 
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




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57