«« Next » « Previous
«« Next » « Previous

Link Details

Link 89655 thumbnail
User 214988 avatar

By puredanger
via tech.puredanger.com
Published: Jun 24 2008 / 03:19

Why does this compile?
  • 19
  • 5
  • 1376
  • 649

Comments

Add your comment
User 283239 avatar

chist replied ago:

0 votes Vote down Vote up Reply

no compile error as JLS: 14.18 The throw Statement
"The Expression in a throw statement must denote a variable or value of a reference type which is assignable (ยง5.2) to the type Throwable, or a compile-time error occurs."

JLS 5.2 Assignment Conversion
>A value of the null type (the null reference is the only such value) may be assigned to any reference type, resulting in a null >reference of that type.

So NPE:

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

User 92 avatar

Neal M Gafter replied ago:

0 votes Vote down Vote up Reply

Although null is assignable to every reference type, the type of null is not itself a reference type. It was our intent that the requirement that the expression be a reference type was to be removed from the third edition of the JLS, but that change never actually made it into the published version. Thus, this is a javac compiler bug which I introduced in SE 5.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.