• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programmer vs. JVM thrown Exceptions (Java OCA 8 Programmer I Study Guide, Sybex)

 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the study guide Chapter 6 is said that:
1. ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, NullPointerException are thrown by the JVM and
2. IllegalArgumentException, NumberFormatException are thrown by programmer.

I don't quite understand this distinction.

I wrote following example:



I think in this case the JVM throws an exception and not a programmer.

Could someone explain it to me please or provide a good link explaining this distinction?

Thank you very much in advance,
Aleksandra
 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to mention about the above example - it throws a NumberFormatException and the book says that it's thrown by programmer.
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksandra Pestova wrote:I think in this case the JVM throws an exception and not a programmer.
  • I think, all the runtime exceptions are thrown by JVM but here when she says exception thrown by JVM and thrown by programmer doesn't meant technically who actually throws it. Technically always JVM throws it but IMO here rather than technically she meant by It's use.


  • When says ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, NullPointerException are thrown by the JVM means those are thrown when there is logical error in programmer's code, It is problem at programmer's side.

  • When says IllegalArgumentException, NumberFormatException are thrown by programmer means It is problem or exception generated because of user's invalid input that programmer can throw by generating these exception.
    • Example of runtime exceptions thrown by programmer:


    If still not satisfied, we should wait for someone who may answer...

    Edit:Apology, If your unable to view program properly because of long line mesages in code, you can hide categories by clicking on button at top left side to see in full screen mode.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aleksandra Pestova wrote:Could someone explain it to me please or provide a good link explaining this distinction?

    The distinction between JVM and programmer thrown exceptions is very subtle and debatable, so it can be very confusing. In this topic I tried to explain the distinction to the best of my ability. Although it's just my (humble) opinion and I am not one of the official exam creators, it might be worth reading.

    If you would still have some doubts, just click the reply-button and let us know

    Hope it helps!
    Kind regards,
    Roel
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Roel  for the link which led to another link, both were really helpful. 
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:When says ArithmeticException, ArrayIndexOutOfBoundsException, ClassCastException, NullPointerException are thrown by the JVM means those are thrown when there is logical error in programmer's code, It is problem at programmer's side.

    I do not completely agree with this. Although I am not a fan, you as a programmer can write code like thisIn this scenario I prefer to use IllegalArgumentException, but it seems the JDK uses NullPointerException as well in these situations (e.g. in Java 7 a new class Objects was introduced (in the java.util package), its (static) method requireNonNull throws a NullPointerException if the object is null). And the same applies to (Array)IndexOutOfBoundsException.

    Ganesh Patekar wrote:When says IllegalArgumentException, NumberFormatException are thrown by programmer means It is problem or exception generated because of user's invalid input that programmer can throw by generating these exception.

    I don't agree with this statement. As a developer you can perfectly use e.g. the IllegalArgumentException to force correct usage of the library/API/application/class you have developed. So another developer will immediately know he/she is using your code incorrectly (if you provide a meaningful error message). For example (recycling one of your own examples )If you expect from a user some input, you should validate this user input. In your example you expect a valid integral number. So you should write code which prevents the NumberFormatException from being thrown at runtime. Regular expressions (not on the OCAJP exam) are a very strong technique for data validation. Let's rewrite your age exampleI would not throw a runtime exception in this case. I would throw a checked exception (e.g. InvalidValueException), because it is an invalid value from the user and thus it's not a programming error/bug (made by the developer). Thus the application can recover from this and if the user enters a valid value, he/she can continue with the application. And the checked exception forces the application to handle this exception (whereas an unchecked/runtime exception) might go unnoticed (and result in a stack trace in the logs which nobody will ever look at )

    Hope it helps!
    Kind regards,
    Roel
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Indeed helped me, It will be much helpful because I'm going to revise exception topic.
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Java Notepad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much Roel and Ganesh for your discussion!

    In my opinion this distinction is very confusing, so seems it's better just to memorize it for the exam.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic