Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
Daniel T. Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3398
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Daniel T. Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic