## CPSC 170 Lab 4: Exceptions

### Placing Exception Handlers

The file ParseInts.java contains a program that does the following:

• prompts for and reads in a line of input
• uses a second Scanner to take the input line one token at a time and parses an integer from each token as it is extracted. This is different -- make sure you understand it
• sums the integers
• prints the sum

Save ParseInts to your directory, compile and run it. If you give it the input:

10 20 30 40

It should print:

The sum of the integers on the line is 100.

Now try a line that contains both integers and other values, for example:

We have 2 dogs and 1 cat.

You will get a NumberFormatException when it tries to call parseInt on "We", which is not an integer. One way around this is to put the loop that reads inside a try block and catch the NumberFormatException, but not do anything with it. This way if it's not an integer it doesn't cause an error; it goes to the exception handler, which does nothing. Do this as follows:

• Modify the program to add a try statement that encompasses the entire while loop. Catch a NumberFormatException and have an empty body for the catch.
• Compile and run the program and enter a line with mixed integers and other values. You should find that it stops summing at the first non-integer, so the line above will produce a sum of 0, and the line "1 fish 2 fish" will produce a sum of 1. This is because the entire loop is inside the try, so when an exception is thrown the loop is terminated. To make it continue, move the try and catch inside the loop. Now when an exception is thrown, the next statement is the next iteration of the loop, so the entire line is processed. The dogs-and-cats input should now give a sum of 3, as should the fish input.

### Throwing Exceptions

The file Factorials.java contains a program that calls the factorial method of the MathUtils.java class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for larger integers and that it always returns 1 for negative integers. Returning 1 as the factorial of any negative integer is not correct mathematically, the factorial function is not defined for negative integers. To correct this, you could modify your factorial method to check if the argument is negative, but then what? The method must return a value, and even if it prints an error message, whatever value is returned could be misconstrued. Instead, it should throw an exception indicating that something went wrong so it could not complete its calculation. You could define your own exception class, but there is already an exception appropriate for this situation IllegalArgumentException, which extends RuntimeException. Modify your program as follows:

1. Modify the header of the factorial method to indicate that it can throw an IllegalArgumentException.
2. Modify the body of factorial to check the value of the argument and, if it is negative, throw an IllegalArgumentException. Note that the expression that follows throw is actually an instance of the IllegalArgumentException class, and that the constructor takes a String parameter. Use this parameter to be specific about what the problem is.
3. Compile and run the Factorials program after making these changes. Now when you enter a negative number an exception will be thrown, terminating the program. The program ends because the exception is not caught, so it is thrown by the main method, causing a runtime error.
4. Modify the main method in your Factorials class to catch the exception thrown by factorial and print an appropriate message, but then continue with the loop. The message should include only the message that you passed with the exception, not the exception itself!

Returning a negative number for values over 16 also is not correct. The problem is arithmetic overflow. The factorial is bigger than can be represented by an int. This can also be thought of as an IllegalArgumentException this factorial method is only defined for arguments up to 16. Modify your code in factorial to check for an argument over 16 as well as for a negative argument. You should throw an IllegalArgumentException in either case, but pass different messages to the constructor so that the problem is clear.

### File I/O

Remember the CD Collection from program from lab last semester? The program had a command line interface that allowed you to add CDs to a collection and perform searches on the collection. If you don't remember you can Download the files Tunes.java, CDCollection.java, and CD.java in order to remind yourself. One problem with this program is that it each time it is run the collection is reinitialized to the same default collection. The program does not have a persistent state, so you are going to add one. Add to this program the ability to read and write a CD collection from a text file.

The file representation of a CD collection for this program will list the title, artist, cost, and number of tracks each on a separate line. This will allow you to use the nextLine method of the Scanner class to parse the file. The file ExampleCollection.txt is an example of a collection text file. You will need to do the following:

1. Add a prompt when the program first runs that asks the user to enter a file name of a CD collection.
2. Replace CDCollection constructor with a constructor that takes a string that is the name of a file that contains a CD collection.
• The file name should be stored in an instance variable and the contents of the file used to initialize the other instance variables.
• If the file does not exist or is not valid (does not have the correct number of lines, the number of tracks is not an integer, etc.) the collection should be initialized to contain no CDs. Use exceptions to handle this!
• You can look at the Square class from lab 2 for some help on how to read text from a file.
3. Replace the code in the Tunes class that initializes the Collection with 6 CDs to call the CDCollection constructor with the file name specified by the user.
4. Add a write method to the CDCollection class.
• The method should write a string representing the collection to the text file specified in the constructor.
• If it is unable to write the file it should throw an error so that the calling method can inform the user.
• You can look at the file PrintWriterDemo.java for an example of how to write to a file.
5. Add code to the Quit case in the Tunes class so that before exiting the collection is written to the file that was opened. This code should utilize a try/catch block to inform the user if there is an error when writing the file.

To submit your code: Tar the files in your lab4 directory and copy the tgz file to the directory /home/staff/bouchard/CPSC170A/lab4. Be sure to name the tar file with your names, not lab4.tgz.