• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Nested Try Catch

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm pretty new to java and I have nested Try Catch code that prints 3 times! can you tell me why?




here is the result:

The entered value aaa is not a valid integer. Please try again.
The entered value 0 is out of range [1 TO 12].
The entered value 5000 is out of range [1 TO 4000].
The entered value aaa is not a valid integer. Please try again.
The entered value 0 is out of range [1 TO 12].
The entered value 5000 is out of range [1 TO 4000].
The entered value aaa is not a valid integer. Please try again.
The entered value 0 is out of range [1 TO 12].
The entered value 5000 is out of range [1 TO 4000].
 
Niloo Rad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


took my validated method from main mehod for loop
 
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Your nested try may be unnecessary if you get your int from a Scanner in a utility method. Search my posts for “Scanner hasNextInt” and you will probably find something useful.
You should avoid catching plain simple Exception. In that case you may suffer a NumberFormatException, so look for that. Actually, the utility method will obviate any Exceptions.
If your values are outwith the required range, you should not be catching anything, but throwing something. An IllegalArgumentException, mots probably.
 
This tiny ad is guaranteed to be gluten free.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic