aspose file tools
The moose likes Beginning Java and the fly likes Nested Try Catch Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Nested Try Catch" Watch "Nested Try Catch" New topic
Author

Nested Try Catch

Niloo Rad
Greenhorn

Joined: Oct 17, 2012
Posts: 6
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

Joined: Oct 17, 2012
Posts: 6


took my validated method from main mehod for loop
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32701
    
    4
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Nested Try Catch
 
Similar Threads
StringTokenizer(read from text File) help needed
Constructor Chaining
connecting java application to JAVADB database
Code without try-catch, just if/else or other
Pretty new to Java, require wisdom. (Long)