e==null? really?

B

bob smith

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

Eric Sosman

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

markspace

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

Daniel Pitts

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

Roedy Green

} 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)
 
L

Lew

markspace said:
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.
 
L

Lew

Eric said:
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 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.
 
J

Joerg Meier

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
 
J

javax.swing.JSnarker

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;
 
D

Daniele Futtorovic

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

Roedy Green

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)
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top