ITEC 120
Treasure Chest, with Exceptions

Objectives

To successfully complete this lab you will perform several operations with custom Exceptions. First, you will add conditions for which an existing custom Exception is thrown. Next, you will create a new custom Exception and integrate it to existing code. Finally, you will create a new type of Driver class that will make use of both custom Exceptions.

Assignment

Download ExceptionsLab.zip, unzip the files into a directory, and compile the existing classes.

Notice the java files: Treasure, TreasureChest, InvalidTreasureException, and the driver, TCFileDrv.

Notice also you have 5 data files: chest1.txt - chest5.txt.

Execute TCFileDrv and walk through the code while examining the output. Run the driver with each of the data files as input. Make a note of what happens for each of the input files. Examine the differences between the files and the effect that they have on program output. There are pieces of data that are able to get through that are clearly inappropriate. Also, when things go well, the program works well, but when there are problems in the data file, the output could certainly be more helpful.

InvalidTreasureException

InvalidTreasureException is a custom Exception. It extends java.lang.Exception, so by inheritance contains lots of functionality that we will find very useful. We can throw and catch it just like any other Exception.

Note that one of the Treasure constructors will throw an InvalidTreasureException: if the name is less than two characters. (You noticed this when you ran the TCFileDrv with chest2.txt as an input, right?)

There are two other conditions noted in the comments above that constructor - one for the weight and one for the value. Modify the existing code to throw InvalidTreasureException when either of those conditions are met. Compile, and run the driver with each of the input files.

Before making that change, the data in chest3.txt loads without any issues. What happens after the changes are made?

There is a very different problem when loading data from chest4.txt. What causes it? NoSuchElementException is meaningless to a programmer who is using the the Treasure and TreasureChest classes in their application. Modify the existing code to catch the NoSuchElementException and throw an InvalidTreasureException instead.

TreasureChestException

When you loaded data from chest5.txt, you certainly noticed the cryptic ArrayOutOfBoundsException. Once again, this is information that is not relevant to a programmer who is using these classes in their applications- they care about treasures and chests and things like that, not whatever the internal workings of these classes are!

Create a class named TreasureChestCapacityException that is thrown when one attempts to add a treasure to a TreasureChest that is already full. Don't forget that any method that throws a given Exception must declare it in the method header.

Loading the TreasureChest

Here's another way we can make our classes more useful to other people using them. While loading a TreasureChest from a file, if an InvalidTreasureException occurs, print an error message that includes the name of the file that was being loaded when the Exception occurred, then continue loading. The message should be printed to System.err, as opposed to System.out.

Continuing along those lines, while loading the TreasureChest from a file, if a TreasureChestCapacityException occurs, print an error message that includes the name of the file and the line number on which the Exception occurred. Again, the message should be printed to System.err, as opposed to System.out.

TCManualDrv.java class

Create a drive class that loads a TreasureChest manually. Class TCManualDrv.java should load each of the items below into a TreasureChest then print the TreasureChest.

name:Shield
description:Engraved shield crafted in Austria in 1582
weight:12.6
value:9800
-----------------------------------------------
name:Gemstone
description:Large Ruby
weight:0.96
value:2600
-----------------------------------------------
name:Puppy
description:2 week old Golden Retriever
weight:6.0
value:1000000

If your class encounters an InvalidTreasureException or TreasureChestCapacityException during the load process, it should print an informative message to System.err and continue loading the TreasureChest, and print out the contents of the TreasureChest when it is finished loading. You must test your class with appropriate data (different from the data above) to be sure that it handles the error situation properly.

Submit Your Assignment

Submit Treasure.java, TreasureChest.java, TreasureChestCapacityException.java, and TCManualDrv.java to the Lab Submission Folder on D2L.