Exceptions. Please help.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Exceptions. Please help.

  1. #1
    Join Date
    Mar 2006
    Posts
    2

    Exceptions. Please help.

    Hi, i'm new to java (which is why im posting it here ). I was just wondering if someone can write me a sample code for exception. Nothing fancy, just a few lines. Lets say ive got my main function and thats doing something. In my main , im asking a user to input their first name (of course, this is string). What i want to do is, if the user has entered in nothing or if they've entered in a number (as a person cannot have numbers in their name), then i want an exception just saying that "You have entered a number as your first name, please try again". Basically, i want my exceptions to be handled in another class completely and i would like them to be triggered when that happens. Could someone help me please? Thanks.

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    When you throw an exception, the operation is just canceled usually. If you just want to ask the user to try again due to invalid input, you don't really need to worry about exceptions.

    Code:
    public class TestException
    {
        private String name = "";
        
        public TestException(String name)
        {
            this.name = name;
        }
        
        public void setName(String s) throws Exception
        {
            if (s == null || s.equals(""))
                throw new Exception("String is either null or empty");
        }
    
        public static void main(String[] args)
        {
            TestException test = new TestException("johnny");
            
            try{
                test.setName("");
            }
            catch(Exception e){
                System.out.println(e);
            }        
        }
    }
    The while loop will continue until the user has entered valid input.
    Code:
    String input = "";
            
            while (input == null || input.equals(""))
            {
                //prompt user for input...
                
                //validate input
                if (input == null || input.equals(""))
                    System.out.println("Sorry, invalid input. Please try again.");
            }

Similar Threads

  1. Replies: 0
    Last Post: 03-10-2003, 09:36 AM
  2. Help exceptions
    By Lenok in forum Database
    Replies: 2
    Last Post: 09-26-2002, 12:31 PM
  3. Replies: 0
    Last Post: 03-15-2002, 03:05 AM
  4. Exceptions vs. Return Values
    By Michael Welch in forum .NET
    Replies: 4
    Last Post: 03-05-2002, 10:07 PM
  5. Exceptions
    By o11011 in forum .NET
    Replies: 3
    Last Post: 01-24-2002, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center