JMU
Lab: Gaining Experience Creating Exception Classes


Instructions: Answer as many of the following questions as you can during the lab period. If you are unable to complete the assignment during the lab period it is strongly recommended that you complete it on your own.

Getting Ready: Before going any further, you should:

  1. Make a directory for this lab.
  2. Download the following files:
    to the appropriate directory/directories. (In most browsers, the easiest way to do this is by right-clicking on each of the links above.)

1. Review: This part of the lab is a brief review of exception handling in Java.
  1. Add an explicit value constructor to the PersonReader class that is passed a File object and instantiates scanner. What code did you add?


        public PersonReader(File file) throws FileNotFoundException
        {
           scanner = new Scanner(file);
        }
    
    Expand
  2. Compile the PersonReader class to make sure your changes are syntactically correct. What exception did you have to catch or specify (i.e., re-throw)?


    FileNotFoundException java.lang.FileNotFoundException
    Expand
  3. Should you catch this exception or specify it (i.e., re-throw it)? Why?


    You should specify it because there is no way for you to correct the problem.
    Expand
  4. The Integer.parseInt(java.lang.String) java.lang.Integer#parseInt(java.lang.String) method throws NumberFormatException java.lang.NumberFormatException . Why don't you have to catch or specify (i.e., re-throw) this exception?


    It is an unchecked exception (because it is a descendant of RuntimeException java.lang.RuntimeException .
    Expand
2. Creating Exception Classes: This part of the lab will give you some experience with creating exception classes (and using them).
  1. Modify the createPerson(String) method in the Person class so that it throws a NumberFormatException java.lang.NumberFormatException if the age portion of the String cannot be converted to an int. What code did you add?


    public static Person createPerson(String s) throws NumberFormatException,
    
    Expand
  2. Modify the readPerson() method in the PersonReader class so that it catches the NumberFormatException and creates a fictitious person with default values for the various attributes. What code did you add?


           try
           {
              result    = Person.createPerson(line);
           }
           catch (NumberFormatException nfe)
           {
              result    = new Person("Fictional Person",65,
                                     "540","568","1671");          
           }
        
    Expand
  3. Modify the createPerson(String) method in the Person class so that it also throws a NumberFormatException if the area code, exchange, or extension do not represent valid integers. (Hint: You don't have to add a throw statement.) What code did you add?


           // Check the area code, exchange, and extension
           Integer.parseInt(areaCode);
           Integer.parseInt(exchange);
           Integer.parseInt(extension);
        
    Expand
  4. Create a checked NumericValueException class that only has a default constructor. The resulting Exception object must have an appropriate description. What code did you write?


    public class NumericValueException extends Exception
    {
        public NumericValueException()
        {
           super("Invalid numeric value");
        }
    }
        
    Expand
  5. Modify the createPerson(String) method in the Person class so that it no longer throws a NumberFormatException if the age is not an int. Instead, it must now throw a NumericValueException. (Hint: You will need to catch the NumberFormatException.) What code did you add?


        public static Person createPerson(String s) throws NumberFormatException,
                                                           NumericValueException
        

    and

           try
           {
              age       = Integer.parseInt(token);
           }
           catch (NumberFormatException nfe)
           {
              throw new NumericValueException();          
           }
        
    Expand
  6. Modify the readPerson() method in the PersonReader class so that it catches the NumericValueException and creates a fictitious person with default values for the various attributes. What code did you add?


           catch (NumericValueException nve)
           {
              result    = new Person("Fictional Person",65,
                                     "540","568","1671");          
           }
        
    Expand
  7. Add an explicit value constructor to the NumericValueException class that is passed an int. The resulting Exception object must have a description that includes the text "Invalid numeric value: " and the parameter value. (Note: You will use this constructor later.) What code did you add?


        public NumericValueException(int n)
        {
           super("Invalid numeric value: " + n);
        }
        
    Expand
  8. Modify the createPerson(String) method in the Person class so that it prints a message and then re-throws the NumberFormatException if the age is not an int and throws a NumericValueException if the age is an int and less than 18. What code did you add?


           try
           {
              age       = Integer.parseInt(token);
    
              if (age < 18) throw new NumericValueException();
           }
           catch (NumberFormatException nfe)
           {
              throw nfe;          
           }
        
    Expand
  9. Modify the readPerson() method in the PersonReader class so that when it catches the NumericValueException it creates a fictitious person named "Young Person" with default values for the other attributes. What code did you add?


           catch (NumericValueException nve)
           {
              result    = new Person("Young Person",65,
                                     "540","568","1671");          
           }
        
    Expand
  10. Add an explicit value constructor to the NumericValueException class that is passed a Throwable that is used as the cause. What code did you add?


        public NumericValueException(Throwable cause)
        {
            super("Invalid numeric value", cause);
        }
        
    Expand
  11. Modify the createPerson(String) method in the Person class so that it throws a NumericValueException that contains the NumberFormatException as the cause if the age is not an int. What code did you add?


           try
           {
              age       = Integer.parseInt(token);
    
              if (age < 18) throw new NumericValueException(age);
           }
           catch (NumberFormatException nfe)
           {
              throw new NumericValueException(nfe);
           }
        
    Expand
  12. Modify the readPerson() method in the PersonReader class so that when it catches a NumericValueException without a cause it creates a fictitious person named "Ageless Person" with default values for the other attributes. (Hint: Use the inherited Throwable.getCause() java.lang.Throwable#getCause() method.) What code did you add?


           catch (NumericValueException nve)
           {
              if (nve.getCause() == null)
              {
                 result    = new Person("Young Person",65,
                                        "540","568","1671");          
              }
              else
              {
                 result    = new Person("Ageless Person",65,
                                        "540","568","1671");          
              }
           }
        
    Expand

Copyright 2016