• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Subtle errors can happen in a try..catch

 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That one i found it the hard way so  i put it here for others to take vision and possibly avoid

Assume you call that method from some other method in your project where you had setup a Scanner object for user's input
Method's logic is simple iterate on input's stream till you get a valid integer. In case of invalid user's input i.e. when nextInt() can’t convert input's string to an integer,
throw an InputMismatchException

What you get if you run the following method's code?  an infinite loop right? but why? ...



... because from API documentation

...When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method...



So got you the trick?
nextInt() was leaving the invalid user input into the stream(I instead thought had to move on) and after catch's return in subsequent while's iterations nextInt() clearly read the same invalid character(and not the fresh one that should be provided by the user in the new iteration) throw his exception(ever leaving the same  invalid user input into stream)  and so on ad infinitum.

Ok found the culprit how to resolve?
It's trivially enough a  sc.next() between lines 6 and 7; that consume(gets rid) the invalid character and so now nextInt() can read correctly the fresh user's input
 
Marshal
Posts: 80639
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But there is no need ever to catch such en Exception. You use a loop to avoid that exception.
That is only an infinite loop if there is an error. It is actually a precondition on the reading; if the precondition is not fulfilled, the reading is allowed never to terminate.
input ∈ {String.valueOf(ℤ) | return sc.nextInt();
 
Harry Kar
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:But there is no need ever to catch such en Exception. You use a loop to avoid that exception.
That is only an infinite loop if there is an error. It is actually a precondition on the reading; ...



Yeah i know all that . Put it exemplified and death clear as in my earlier post seems trivial but  if you put it into some thousands lines of code I can assure you it's not at all
The code  is not mine i only reviewed it and wanted share that possible subtle situation.

For who follow the discussion what CR refers in

But there is no need ever to catch such en Exception. You use a loop to avoid that exception.


mean that is more natural instead of the code in my earlier post write the following:  



 
Campbell Ritchie
Marshal
Posts: 80639
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harry Kar wrote:. . .

Please check whether that code is correct; calling nextLine() may have unwanted side‑effects.
 
Harry Kar
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Harry Kar wrote:. . .

Please check whether that code is correct; calling nextLine() may have unwanted side‑effects.



Please explain better
 
Campbell Ritchie
Marshal
Posts: 80639
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if the user has entered a number, realised there was an error and tried to correct it?

The User of the input wrote:123x 124

. . . adding the x to 123 because it was incorrect.
reply
    Bookmark Topic Watch Topic
  • New Topic