• 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

What is the right try/catch for an InputMismatchException?

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this exception is not correct because I get that long java.util.InputMismatchException error. The program still allows the user to re-enter but I obviously don't want the long error. What's the actual way to do it?

 
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So remove the e.printStackTrace();
Also, if you want to ignore only the InputMismatchException then that should be the one you catch and ignore not the blanket Exception you are catching.
 
Marshal
Posts: 79174
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never need to catch an InputMismatchException because, as Rob Spoor points out, you can always avoid it. There should be no need to check hasNext() if you use System.in because you should never close a reader or scanner pointing to System.in.

Never use == true or == false, which are poor style and very error‑prone. It's while (flag)... or while (!flag)...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic