Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

error checking

 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been told that we don't need to worry about exception handling in the very first "java in general" exercises, but I'm somewhat confused - or I've confused myself or something. Does this mean that until told otherwise we should assume that the user did everything right? Meaning, for example, that in Java-4a I should assume that the user entered exactly one integer between 0 and 99 along with the program name and just trek along?

Thanks!
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, exception handling is one thing - error checking is another.
You can do quite simple checks for range without going ahead and throwing exceptions (which changes the flow of the program pretty considerably).

On the other hand, the assignment doesn't specify that you need to make sure that the input is clean. At this particular point in time, if the user puts in their dog's first name rather than an integer, the program can crash and burn in all its glory.

- does this just about cover it, Marilyn?
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jinny Morris:
I've been told that we don't need to worry about exception handling in the very first "java in general" exercises, but I'm somewhat confused - or I've confused myself or something. Does this mean that until told otherwise we should assume that the user did everything right? Meaning, for example, that in Java-4a I should assume that the user entered exactly one integer between 0 and 99 along with the program name and just trek along?


You don't need to worry about exception handling such as the case where the user passes in "ab" instead of "12", which would throw an Exception trying to parse "ab" as a number. You do, however, need to handle range checking as the assignment says. You can do that without using exceptions.

Try thinking along the lines of the way the instructor solutions handle checking whether user input exists or not without handling the NullPointerException that could be thrown if the user doesn't provide an argument.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic