Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: e==null? really? Date: Fri, 13 Jul 2012 13:11:18 -0700 (PDT) Organization: http://groups.google.com Lines: 49 Message-ID: <57cde03f-92eb-48bf-9da8-ced150c0f921@googlegroups.com> References: NNTP-Posting-Host: 69.28.149.29 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1342212639 1132 127.0.0.1 (13 Jul 2012 20:50:39 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 13 Jul 2012 20:50:39 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Received-Bytes: 2451 Xref: csiph.com comp.lang.java.programmer:16007 Eric Sosman wrote: > markspace wrote: > > 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 (=A75.2) to the type > > Throwable, or 2) the null reference..." >=20 > Interesting. Reading onward, though, we find >=20 > "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. [...]" >=20 > ... 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 ru= le. > try { > throw null; > } catch (Exception e) { > // e is null here! > } >=20 > ... 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=20 doesn't. --=20 Lew