Create a main method to demonstrate the jvm throwing each of these exceptions. You might have to dig around a bit to find out what they are and who (what methods) throw them. Note that you are not going to create an Exception of this type and throw it, you will write a snippet of code that causes one to be thrown.
ArrayIndexOutOfBoundsException - eg. Create an array and access an element that is not there will cause this one to be thrown.
ClassCastException
IllegalArgumentException
NullPointerException
NumberFormatException
FileNotFoundException
NegativeArraySizeException
These 2 might be tough... do them if you like a challenge.
StackOverflowError
NoClassDefFoundError
Part 3
Demonstrate the use of multiple catch blocks as follows:
there is one line of code below that might throw an ArithmeticException, or an ArrayIndexOutOfBoundsException.
Random r = new Random();
int[] array = {10, 20};
int result = array[r.nextInt(array.length + 1)] / r.nextInt(2);
Explain why each of these might happen.
Put this code in a try block that can distinguish between which one has been thrown (2 catch blocks). Run the code until you have demonstrated all 3 possibilities:
1. no exceptions,
2. ArrayIndexOutOfBoundsException
3. ArithmeticException
Part 4
Write a method called average() that accepts an array of integers called grades.
If the method does not throw an exception, it will return the average of the grades in the array. Start by implementing that.
The method will throw an exception of type InvalidGradeException if any element of the array is greater than 100 or less than 0.
You must create the InvalidGradeException class.
The InvalidGradeException object should contain information about what went wrong, i.e., which of the grades was invalid (its index), and what the invalid value was. The exception will be caught by the caller (the main method) and print out the information contained in the InvalidGradeException object.
You are demonstrating that you can pass information from a method back to the caller, by putting it in an Exception object and throwing it.
The average() method will change all the grades in the array to 60 that are less than 60, after the average is calculated. This will also be done if an exception is thrown, and the average is not calculated. This will be done in a finally block INSIDE the average method.
Demonstrate that the average method will work for an int array of various sizes, even though the method has only one parameter.
Demonstrate that your method will work with an array that has no invalid grades, AND with an array that has invalid grades.