CSC1110 Homework 14
Exceptions
Exceptions are thrown whenever the compiler or the virtual machine encounters a command they cannot unambiguously process. Compile time exceptions are easy to detect, as the compiler will not allow the program to successfully compile when they exist. Runtime exceptions are harder to anticipate. They are often the result of some external value, such as user input, missing files, or bad network connections. For this assignment, you will be adding exception handling to existing code to make it function properly.
Exception Handling
There are two ways you can deal with exceptions:
- Use a try/catch block where the exception occurs
- Use a try/catch block higher up the call stack, where the code that is currently running was called from
In general, you will catch the exception where the cause was introduced. This is not always where the exception was generated. An example would be if a user entered an invalid input of some sort and that input was passed to a helper method, where an exception occurs. The root of the cause was the user input, and the helper method may not be able to deal with the exception appropriately, so the helper method would not catch the exception and let it be thrown up the call stack to the method that had the user input, where it would be caught and handled.
Exceptions and Javadocs
If there is ever a time where a method may not catch an exception that can occur in the method, you are required to add a throws
statement to the method declaration like this:
public int someReallyDifficultMath() throws IllegalArgumentException
When this happens, you also must add a @throws
annotation to the javadocs describing when the exception would be thrown.
/** * Transmogrifies the Zorg matrix and returns the inferred Mok value * * @return the inferred Mok value * @throws IllegalArgumentException if a value in the Zorg matrix is invalid **/
NumberListDriver and NumberList
The program will consist of a NumberListDriver
class and a NumberList
class. Both of these
classes are implemented and function correctly when no unexpected input is given. You will add exception handling to both of the classes that will produce identical output to the samples given below. Run the code with the given input to see what kind of exception is being thrown. Then use the debugger to see where the exceptions are being thrown from and handle them at the appropriate location. The following are additional requirements:
- If a non-numeric value is given, the program should inform the user, ignore that value, and continue reading values
- If more than the maximum amount of values are entered, the program will inform the user and reprompt for input
- If a floating point value is entered, the program will inform the user that it will round down to the nearest integer, then round the value down and add it to the list
Sample Runs
Enter a series of up to 20 whole numbers separated by a space: 1 2 3 4 5 Mean = 3 Enter a series of up to 20 whole numbers separated by a space: No valid values entered. Exiting. Enter a series of up to 20 whole numbers separated by a space: 1 g 5 8 Bad input: For input string: "g". Skipping. Mean = 4 Enter a series of up to 20 whole numbers separated by a space: 1 2.2 3 Floating point number detected: 2.2. Rounding down to 2. Mean = 2 Enter a series of up to 20 whole numbers separated by a space: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Too many numbers. You may only input 20 numbers. Enter a series of up to 20 whole numbers separated by a space: 5 7 9 Mean = 7
Submission Instructions:
Commit and Push your files to GitHub Classroom.