File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic

Exception Handling

Daniel T. Williams

Joined: Sep 21, 2013
Posts: 6
Hello, I'm pretty new to this and I'm trying to learn exception handling right now. This is part of an assignment I'm working on but the problem I'm running into is with the second prompt. When the user enters a time, if it is a string it should throw an exception, it does do this but when the loop starts again the string gets stored in the name variable which I don't want. Any thoughts or suggestions would be appreciated. Thank you.

K. Tsang

Joined: Sep 13, 2007
Posts: 3131

One simple approach is to blank the "name" variable just before you throw the exceptions.

If the name variable is not cleared, does your program prompt the "when is your appointment" line when the exception is thrown?

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Daniel T. Williams

Joined: Sep 21, 2013
Posts: 6
I have tried blanking the name variable but that hasn't done anything. Every time it will ask the user for "The appointment time to be entered". I tried stepping through each line of code and the string that causes the error, it gets added to the name variable once it loops back and starts over again.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46353
If the time is invalid, throw the Exception. Don't try to catch it.
The problem you have is that Exceptions are designed to show that an error has occurred and the method cannot complete. They will only work if you have several methods. Trying to throw an Exception and catch it in the same method is simply an ineffieicnt way to implement an if-else. Don't do it.
I agree. Here's the link:
subject: Exception Handling
It's not a secret anymore!