Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A null exception in the catch header

 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the OCAJP 7 Summary from Bagdouri Mohammed Amine is written, that if the JVM finds a null exception in the catch header, a NullPointerException will be thrown, but how you can reach it, that you get a null exception in the catch header?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamil Hlubek wrote:In the OCAJP 7 Summary from Bagdouri Mohammed Amine is written, that if the JVM finds a null exception in the catch header, a NullPointerException will be thrown

That's not correct!

A throw statement is defined in the JLS as:If Expression evaluates to null, then a NullPointerException is thrown. Here's the appropriate rule from the JLS
JLS, 14.18. The throw Statement wrote: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. The throw statement then completes abruptly, the reason being a throw with value V'.


Here is an example illustrating the aboveIf you run this class, the output will be:
java.lang.NullPointerException
at NullException.main(NullException.java:4)


So reference variable e (in the catch block) is not null, it's referring to the NullPointerException which is thrown at line1 (instead of null which is according to the JLS).

Hope it helps!
Kind regards,
Roel
 
Kamil Hlubek
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic