I saw one reference for a code in Java which had a line:
throw null;
I tried the same and there is no compilation exception, till now I thought we can only throw exception, I tested that I can throw any number or Object. Why I am not getting compilation exception? If I try to throw String it gives the below exception what does this means?
No exception of type String can be thrown; an exception type must be a subclass of Throwable
Can anyone please explain the reason and use cases where I can use the above code line?
class Solution {
public int repeatedNTimes(int[] A) {
for (int i = 0; i < A.length - 1; ++ i) {
if (A[i] == A[i + 1]) { // any two neighbour numbers
return A[i];
}
}
// could be evenly distributed excluding the above case
for (int i = 0; i < A.length - 2; ++ i) {
if (A[i] == A[A.length - 1] || A[i] == A[A.length - 2]) {
return A[i];
}
}
throw null; // input array is not what has been described
}
}