class ThrowNull {
public static void main(String[] args) {
throw null;
}
}
We know that rule for throw is throw ThrowableInstance;, where ThrowableInstance must be an object of type Throwable or a subclass of Throwable.
Simple types, such as int or char, as well as non-Throwable classes, such as String and Object, cannot be used as exceptions. null is a special Java literal which represents a null value.
So why would throw null; compile in this code?
String a() {return null}must be an object of (sub)class or String. Simple classes, like Throwable or BigInteger cannot be used as Strings. Null is a special Java literal which represents a null value. Then whyreturn nullis not creating any compile time error?????!!!! I am exposing your double standards here. Why do you complain aboutthrow nullbut not aboutreturn null? I cannot upvote your question therefore. It is too localized by means of double standards.