• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Try Catch

 
Niloo Rad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


took my validated method from main mehod for loop
 
Campbell Ritchie
Sheriff
Pie
Posts: 49842
70
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic