Why is “throw null” not creating a compilation error in Java?

According to the language specification, a throw statement is defined as:

throw Expression

And if the Expression evaluates to null, then a NullPointerException is thrown. Specifically,

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.

Since NullPointerException extends RuntimeException, it is an unchecked exception. This could explain why there’s no compile-time error reported from this construct.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)