What happens when I run this?
The response is:
I was surprised that this did not result in a compile-time error.
Why can I throw null in Java, and why does it upcast it to a NullPointerExce ption?
I'm not sure if this is considered an "upcast" since I'm passing a null value.
I cannot think of any valid reasons to use a null value in an interview.Maybe you're hoping to get fired, but why would anyone purposely do something that would result in their dismissal? I went through this resource but didn't get it.
Code:
public class WhatTheShoot {
public static void main(String args[]){
try {
throw null;
} catch (Exception e){
System.out.println(e instanceof NullPointerException);
System.out.println(e instanceof FileNotFoundException);
}
}
}
Code:
true false
Why can I throw null in Java, and why does it upcast it to a NullPointerExce ption?
I'm not sure if this is considered an "upcast" since I'm passing a null value.
I cannot think of any valid reasons to use a null value in an interview.Maybe you're hoping to get fired, but why would anyone purposely do something that would result in their dismissal? I went through this resource but didn't get it.
Comment