Results 1 to 2 of 2
  1. #1
    Sheriff is offline Member
    Join Date
    Feb 2015
    Posts
    2
    Rep Power
    0

    Default Creating class that throws errors

    I created a class Person:
    Java Code:
    public class Person {
        protected static int MAX_AGE = 150;
        protected static int MIN_AGE = 0;
    
        private static final String VALID_WORD_REGEXP = "[a-zA-Z]{3,30}";
    
        private String firstName;
        private String lastName;
    
        private int age;
    
        Person(){
            setFirstName("anonymous");
            setLastName("anonymous");
            setAge(18);
        }
    
        Person(String firstName, String lastName, int age){
            setFirstName(firstName);
            setLastName(lastName);
            setAge(age);
        }
    
        public void setFirstName(String firstName){
            if(isValidWord(firstName)){
                this.firstName = firstName;
            }else{
                throw new IllegalArgumentException("Invalid first name");
            }
        }
    
        public void setLastName(String lastName){
            if(isValidWord(lastName)){
                this.lastName = lastName;
            }else{
                throw new IllegalArgumentException("Invalid last name");
            }
        }
    
        private boolean isValidWord(String word){
            return word.matches(VALID_WORD_REGEXP);
        }
    
        public void setAge(int age){
            if(isValidAge(age)){
                this.age = age;
            }else{
                throw new IllegalArgumentException("Age is not in a range of [" + MIN_AGE + "-" + MAX_AGE + "]");
            }
        }
    
        private boolean isValidAge(int age){
            return age >= MIN_AGE && age <= MAX_AGE;
        }
    
        String getFirstName(){
            return firstName;
        }
    
        String getLastName(){
            return lastName;
        }
    
        int getAge(){
            return age;
        }
    
    }
    I'm trying to learn right way how to throw exceptions. If I'm doing something wrong let me know and tell me how to fix it.

    Besides that I have couple questions:
    1. I can't quite understand throws statement. I'm thinking throws statement is for passing exceptions one level higher(with are not cought in this caller function). So that would mean I need to put throws in this function only:
      Java Code:
      Person(String firstName, String lastName, int age)
      . Am I right or wrong and is there any other use for it?
    2. If so that would mean when
      Java Code:
      Person person = new Person(.....)
      , must be in try tags. How can I avoid it?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Creating class that throws errors

    Have you discovered the Oracle tutorials?
    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Maven throws errors pointing to settings.xml
    By smoothrace in forum Apache Maven
    Replies: 0
    Last Post: 01-28-2015, 07:39 AM
  2. Replies: 4
    Last Post: 04-22-2012, 09:22 PM
  3. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  4. Calling on class with throws IOException
    By gisler in forum Networking
    Replies: 5
    Last Post: 12-01-2009, 04:20 PM
  5. Recursive Class throws NullPointerException
    By freeBatjko in forum New To Java
    Replies: 5
    Last Post: 11-03-2009, 09:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •