File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Nested Try Catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Nested Try Catch" Watch "Nested Try Catch" New topic
Author

Nested Try Catch

Niloo Rad
Greenhorn

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


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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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://aspose.com/file-tools
 
subject: Nested Try Catch
 
Similar Threads
Code without try-catch, just if/else or other
Pretty new to Java, require wisdom. (Long)
connecting java application to JAVADB database
Constructor Chaining
StringTokenizer(read from text File) help needed