• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Chapter 6 Exceptions, Question 11 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output of the following program?


A. 12, followed by a stack trace for a NumberFormatException
B. 124, followed by a stack trace for a NumberFormatException
C. 12456
D. 12456
E. 1256, followed by a stack trace for a NumberFormatException
F. The code does not compile.
G. An uncaught exception is thrown.

Answer at the end is A, but should be A,G. As mentioned in explanation - "... Line 7 throws a NumberFormatException. It isn’t caught, so ..."
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That question doesn't say "choose all that apply" which means you are supposed to choose the best single answer. Choice A includes throwing a specific stack trace so it is the best answer. If the code threw a NullPointerException say, then the answer would be choice G (and not choice A.)

Also, I added the word "Wiley" to the subject of both your threads. There are multiple OCA 8 study guides out now so we want it to be obvious to other posters which threads are about which. I of course knew because I recognized our questions .
 
Jakub Turoboś
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:That question doesn't say "choose all that apply" which means you are supposed to choose the best single answer. Choice A includes throwing a specific stack trace so it is the best answer. If the code threw a NullPointerException say, then the answer would be choice G (and not choice A.)

Also, I added the word "Wiley" to the subject of both your threads. There are multiple OCA 8 study guides out now so we want it to be obvious to other posters which threads are about which. I of course knew because I recognized our questions .



I guess I have to read the questions more carefully. Thanks again ;)
 
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

Jeanne Boyarsky wrote:If the code threw a NullPointerException say, then the answer would be choice G (and not choice A.)


That's not 100% correct If a NullPointerException was thrown inside the try block, the NullPointerException would definitely be caught by the catch block on line 9 and thus it would not have been an uncaught exception. So an ArrayIndexOutOfBoundsException would have been a better example for choice G
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

Jeanne Boyarsky wrote:If the code threw a NullPointerException say, then the answer would be choice G (and not choice A.)


That's not 100% correct If a NullPointerException was thrown inside the try block, the NullPointerException would definitely be caught by the catch block on line 9 and thus it would not have been an uncaught exception. So an ArrayIndexOutOfBoundsException would have been a better example for choice G


Heh. You are right. I didn't actually read the question before replying. Just the answers.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:I didn't actually read the question before replying. Just the answers.


To all OCA exam aspirants: don't try this at the actual exam
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does "int x = Integer.parseInt(name);" throw a NumberFormatException? It should throw a NPE which makes more sense.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look for the API documentation for Java and find the specs for the parseInt(String) method of java.lang.Integer. Observe the part which documents what exceptions it can throw -- then ask yourself why it doesn't ever throw NPE.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Micheal Bush wrote:Why does "int x = Integer.parseInt(name);" throw a NumberFormatException? It should throw a NPE which makes more sense.


Not really. The JavaDocs for the two-argument version of parseInt() clearly states that a NumberFormatException will be thrown if the value to be parsed is null or an empty string. Null or empty string is not parseable to an integer so I don't see why it would be less appropriate to throw a NFE than a NPE. If anything, expecting NPE implies certain assumptions about the implementation of the parseInt() method which, in my opinion, would be inappropriate. Besides, a NPE usually points to a programming error and in this case, a null or empty string is clearly something you would want to check and explicitly flag as unacceptable input.

If one were to argue for a different exception, an IllegalArgumentException might be a more logical alternative. I still concur with choosing NumberFormatException though.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get even more vicious arguemnts about what sort of exception to throw in those circumstances than about whether to use tabs or spaces for indenting
But a NumberFormatException IS‑AN illegal argument exception; look at its inheritance hierarchy.
Maybe people thought that it would be so unusual to pass null to that method that it wouldn't be worth throwing an NPE. Or maybe they had this sort of argument themeselves I would have thought it would be unusual to pass null to that method, but I think I would have preferred to throw an NPE myself, because that doesn't happen from normal inputs.
 
Micheal Bush
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Micheal Bush wrote:Why does "int x = Integer.parseInt(name);" throw a NumberFormatException? It should throw a NPE which makes more sense.


Not really. The JavaDocs for the two-argument version of parseInt() clearly states that a NumberFormatException will be thrown if the value to be parsed is null or an empty string. Null or empty string is not parseable to an integer so I don't see why it would be less appropriate to throw a NFE than a NPE. If anything, expecting NPE implies certain assumptions about the implementation of the parseInt() method which, in my opinion, would be inappropriate. Besides, a NPE usually points to a programming error and in this case, a null or empty string is clearly something you would want to check and explicitly flag as unacceptable input.

If one were to argue for a different exception, an IllegalArgumentException might be a more logical alternative. I still concur with choosing NumberFormatException though.




If you read API of Double.parseDouble, you will find:

Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable double.


 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Micheal Bush wrote:
If you read API of Double.parseDouble, you will find:

Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable double.


Nobody ever accused the authors of the standard library of being consistent or perfect.

Since Integer.parseInt(String s, int radix) is closer to Integer.parseInt(String s) than Double.parseDouble(String s), the case for mimicking the behavior of Double.parseDouble(String s) is weaker than the case for mimicking Integer.parseInt(String s, int radix), in my opinion.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Micheal Bush wrote:
If you read API of Double.parseDouble, you will find:

Throws:
NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable double.


Nobody ever accused the authors of the standard library of being consistent or perfect. . . .

Least of all in the earlier versions of Java®. I would have used that sort of exception myself, but, as I said before, that is a good way to start arguments.

On closer examination of Double#parseDouble(), it transpires that it calls Double#valueOf() behind the scenes, whereas Integer.parseInt("123") calls Integer.parseInt("123", 10). Agree with Junilu: an inconsistency.
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic