• Post Reply Bookmark Topic Watch Topic
  • New Topic

When do we need the try/catch block most?  RSS feed

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying my best to avoid any errors arising from invalid user inputs.
When is the best time to use try/catch blocks?
I know this doesn't make any sense to surround everything inside
them? so what is it?
Thanks
Jack
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you study first about checked and unchecked exception. Your answer you might find yourself, once you know there differences and study example like IO Exception, NumberFormat exception. Lets know, if you get any query ,
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming you are using a GUI and not commandline (although you could, with a bit more work), why not accept all user input as string, and then just one set of code for all field validation. Inevitably, the user will try to enter letters in the numeric field(s). This way you catch the error and not the system.

How does that saying go -- "It's hard to make things foolproof because fools are so ingenious."
 
manish awara
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want user to see the output with simple language and you don't want to show the red error message to user
then try catch block in exception handling will perform it
except it if you want to find the error in your code try catch block is used.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree with all of you. The idea is that a checked Exception is something it may be possible to recover from, and an unchecked exception (or an Error) is something which you cannot recover from. If you suffer an NullPointerException or ArithmeticException, you will probably have to go through the code and correct the error there. In the case of IllegalArgumentException, a method or constructor refuses the argument passed because it would breach the class' invariants.
FileNotFoundException can be sorted out by correcting the file name. IOException might be sorted out by reconnecting a network cable
If you can work out how to recover from the Exception, then it goes in a try‑catch. If you can't work out how to recover, then use an unchecked exception and let the application terminate.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously what I wrote is a gross over‑simplification. NumberFormatException (NFE) doesn't follow those rules; you can sometimes recover by a new entry. When reading from a file, however, an NFE may mean the the format of the file and the format expected by the program are different. You have to be very careful with formats of text files, therefore.
InputMismatchException (IME) is similar, but is thrown by Scanners. If you are using a Scanner to read from the keyboard, there are two ways you can suffer an IME. One is using nextLine after nextSomethingElse, as in this thread. The other is incorrectly formated input, which Rob Spoor points out as totally avoidable:-
Rob Spoor wrote:. . . check for input.hasNext:
I think I forgot the hasNext() before in the loop, but that could cause an exception with the input.next() call if the stream is closed.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Disagree with all of you. [...]


Generally I would agree with you here, that catching the error(s) and exiting the program gracefully is the way to go. But the OP was looking to catch user data entry errors, which should be easily caught and handled before suffering a catastrophic failure.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I have shown how you can avoid all Exceptions by using a method which validates the format of the input.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!