• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

A null exception in the catch header

 
Ranch Hand
Posts: 49
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic