Tuesday, April 27, 2010

[TF003] You can only throw instanceof Throwable in Java (true/false?)

(true/false?) In Java, you can only throw something that is an instanceof Throwable.
FALSE! You can throw null;, and null is NOT an instanceof Throwable.

Note that technically, it's NOT null itself that is thrown at run-time. An excerpt from JLS 14.18:
A throw Expression ; statement first evaluates the Expression. [...] If this evaluation completes normally, producing a null value, then an instance of NullPointerException is created and thrown instead of null.

2 comments:

  1. Have you seen this video by the way?

    http://www.polygenelubricants.com/2010/02/hoare-null-references-billion-dollar.html

    ReplyDelete