wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Daniel T. Williams
Greenhorn

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
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

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/OCM-JD OCPJP7 OCPWCD5
Daniel T. Williams
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
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: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
Shopping cart help
Program to count frequency of each digit in Pi
Having problem when i sort out data from hashmap. Variables are mixing!
2 objects EXCEPTION
what's wrong with the for-each