CPSC 170 Lab 5: Exceptions

Exceptions Aren't Always Errors

The file CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. Save it to your directory and study it, then compile and run it to see how it works. In reading the code, note that the word is converted to all upper case first, then each letter is translated to a number in the range 0-25 (by subtracting 'A') for use as an index. No test is done to ensure that the characters are in fact letters.

  1. Run CountLetters and enter a phrase, that is, more than one word with spaces or other punctuation in between. At the end of the first word, this program will throw an ArrayIndexOutOfBoundsException, because a non-letter will generate an index that is not between 0 and 25. Of course, you could explicitly test the value of the character to see if it is between 'A' and 'Z'. However, an alternative is to treat the translated character as an index, and catch the ArrayIndexOutOfBoundsException when it occurs. When a non-letter appears, it is effectively the end of the word; you want to ignore the rest of the phrase and display the count. Since your only response to a non-letter is to stop processing, the handler will be empty. Modify this method to do this as follows:
  2. Now modify the body of the catch so that it prints a useful message (e.g., "Not a letter") followed by the exception message. Remember that an exception is an object (which happens to have a toString method). Compile and run the program.
  3. Although it's useful to print the exception for debugging, when you're trying to smoothly handle a condition that you don't consider erroneous you often don't want to display the exception explicitly. Notice that when the exception was printed it told you generally what the exception was and then there was a more specific message (i.e. which index was bad). It would be more useful for debugging if the message printed what character caused the exception. You can often extract the specifics of an exception using a method from the exception object. Look at the online Java documentation for IndexOutOfBoundsException to see how to extract these details. (Hint: this class inherits its interesting methods from its superclasses, so you might need to look at some of those to find a useful method.) Use this information to change the print statement to specify the character that created the out of bounds index.

Placing Exception Handlers

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

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 Integer.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:

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:

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.

Reading a User-Selected File

Write a program that prompts the user for a filename, then opens a Scanner to the file and copies it, a line at a time, to the standard output. If the user enters the name of a file that does not exist, ask for another name until you get one that refers to a valid file. Some things to consider:


Hand In: Tar your lab directory and e-mail it to your instructor with cpsc170 lab6 in the subject.