Lab 3: Exceptions

Copyright RIT 2009


Goal

The primary purpose of this lab is to provide you with the opportunity to work with exceptions in Java. You will have a chance to write code which processes exceptions generated by other classes and also to create classes that detect errors and report them via exceptions.


Team Setup

You are to work on this lab completely on your own.

Overview

Objectives

  1. Learn how exceptions are specified in a program.
  2. Write a class that throws an exception.
  3. Learn how to catch exceptions generated by another class.

Ask Dr. Tiger

Pre - Lab Work

  1. Review your class notes for the preceding week.

  2. Read Chapter 17 in Liang.

  3. You can also find additional information on exceptions from this section (http://java.sun.com/docs/books/tutorial/essential/exceptions/) in the Java tutorial.

In-Lab Activities

Each lab you will do is separated into a set of activities. Each activity has specific things you must hand in for credit. Each activity is graded separately.

Activity #1 : Creating an object to handle exceptions

In this activity you will create a class from which exception objects can be instantiated. The name of the class is ShapeException and it will be used to signal errors in the shape classes you will be working with in this lab. To get started, download the jar file that you need to complete this lab by clicking lab3.jar (http://www.cs.rit.edu/~vcss232/pub/lab03/Binaries/lab3.jar) . After unpacking the jar file you will see an RCS directory and several class files. The RCS directory contains the files that you will be working with in this lab.

Read the javadoc description of the ShapeException class, and then write the Java code to implement the class. Test your class using the TestException.java program that is included in the jar file.

How To Submit

When you are sure that your exception class works correctly, check in your file and submit it using the following command:

try grd-232 lab3-1 ShapeException.java

Activity #2 : Exceptions in the shapes classes

In this activity you will be working with simple classes that represent various shapes. Check out the Shape.java and TestCircle.java files from the RCS archive for reading(i.e. don't lock them, you will not change these files). Then check out the Circle.java and Rectangle.java files from the RCS archive for writing. Take some time to study the javadoc pages for Shape, Circle, and Rectangle so that you understand the behaviors of each class.

In this activity you will detect error conditions in the methods associated with a class, and indicate those errors by throwing a ShapeException. You will make additions to the Circle and Rectangle classes.

Reread the javadoc page for the Circle class. You need to find all the methods in the class that throw exceptions and the text string that must be specified for each one (the text string appears in the javadoc throw section comments). Modify the Circle.java code to correctly throw these exceptions. (As a general rule, you want to check inputs and throw exceptions before changing the state of an object.) Use the TestCircle.java program to test your Circle class.

Now add exceptions to the Rectangle class. You must throw ShapeException objects with the specified descriptions when you detect the following conditions:

  1. The constructor is called with a negative width or height parameter ("Invalid parameter in Rectangle constructor").
  2. A negative factor is specified as a parameter to the stretchBy method ("Negative factor in call to stretchBy") Zero is not a negative number.

Note that this time the comments in the file are not already set up for you. Modify the Rectangle class so that the class methods throw the appropriate exceptions. Be sure to modify the comments in the source file to reflect the new exceptions that are thrown. Test your class using TestRectangle.java that you will find in the jar file.

How To Submit

When you are sure that both classes are implemented correctly, check them into RCS and submit your source code using:

try grd-232 lab3-2 Circle.java Rectangle.java 

Activity #3 : Controlling the Tank Farm

In this activity you will write a simple program, named TankFarm.java, that maintains a collection of tanks. In order to understand what this program will do, read the javadoc descriptions for the Tank, TankFarm and TankFarmException classes. In the jar file for this lab you will find a class file that provides an implementation for the Tank class. In this activity you will write the code to implement the TankFarm and TankFarmException classes. This is a straightforward assignment - carefully follow the instructions provided in each method's header.

Check out TankFarm.java for editing. Make the modifications necessary to implement all the exception throws and catches. This will include defining the TankFarmException class yourself.

Simply catching "Exception" is not permitted.

How To Submit

Make sure you write test programs to test your modifications. These test programs will have to create a TankFarm and test that all the exceptions are correctly generated or handled. Do not change the signatures of any methods in the TankFarm class. When you are sure that your TankFarm and TankFarmException classes work correctly submit your files using the command:

try grd-232 lab3-3 TankFarm.java TankFarmException.java 


Grade Computation

Grade Breakdown: