Tuesday, 24 June 2008
Throw null.
« Dont put a lot in IN. | Main
Eugene posed an interesting question about Alex' blog entry
on why the following code does not require main method to throw throwable -
But the reason(or my hypothesis) why the compile dos not require you to add a Throwable to throws clause is as follows -
public class Weird {
public static void main(String[] args) {
throw null;
}
}
The fact that it throws a NPE has been discussed on Alex' blog.But the reason(or my hypothesis) why the compile dos not require you to add a Throwable to throws clause is as follows -
- The null reference can always be cast to any reference type.
- Checked Exceptions should be declared in the throws clause.
- RuntimeExceptions and Descendants are exempt from the above requirement(2)
- Rephrasing 2 and 3 as "if the type is not an instance of RuntimeException", which translates to the following code -
if(!(type instanceof RuntimeException)) { //check throws clause for validity }else { //dont check. }and we can see that 'null' case would go to the else block.
Posted by at 12:27 PM in Thoughts on Java/J2EE
[Trackback URL for this entry]
