Part 2 Create a main method to demonstrate the jvm throwing each of these except
ID: 3785612 • Letter: P
Question
Part 2
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.
*Please label parts that were attempted.
Explanation / Answer
part3)
import java.util.Random;
public class ExceptionTest {
public static void main(String[] args) {
try
{
Random r = new Random();
int[] array = {10,20};
int result = array[r.nextInt(array.length + 1)] / r.nextInt(2);
}catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("arrayindexoutofbounds thrown");
e.printStackTrace();
}
catch(ArithmeticException e1)
{
System.out.println("arithmetic exception");
e1.printStackTrace();
}
}
}
output
arithmetic exception
java.lang.ArithmeticException: / by zero
at ExceptionTest.main(ExceptionTest.java:14)
-----------------------------------------------------------------------------------------------
ARRAYOUTOFBOUNDSEXCEPTION
import java.util.Random;
public class ExceptionTest {
public static void main(String[] args) {
try
{
int[] array = {10,20};
System.out.println(array[5]);
}catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("arrayindexoutofbounds exception thrown");
}
}
}
-----------------------------------------------------------------------------------------
ClassCastExeption
import java.util.Random;
public class ExceptionTest {
public static void main(String[] args) {
try
{
// ClassCastException thrown here.
Object i = Integer.valueOf(42);
String s = (String)i; // ClassCastException thrown here.
}catch(ClassCastException e)
{
throw new ClassCastException("classcastexception thrown");
}
}
}
output
Exception in thread "main" java.lang.ClassCastException: classcastexception thrown
at ExceptionTest.main(ExceptionTest.java:17)
--------------------------------------------------------------------------------------------
IlleagalArgumentExeption
public class ExceptionTest {
public static String test(String name)
{
if(name==null)
{
throw new IllegalArgumentException("an illeagalexception is thrown");
}
else
return name;
}
public static void main(String[] args) {
test(null);
}
}
output
Exception in thread "main" java.lang.IllegalArgumentException: an illeagalexception is thrown
at ExceptionTest.test(ExceptionTest.java:8)
at ExceptionTest.main(ExceptionTest.java:16)
------------------------------------------------------------------------------------------------------------------
NULLPOINTEREXCEPTION
public class ExceptionTest {
public static String test(String name)
{
if(name.equals("david"))
{
System.out.println("null pointerexception");
}
return name;
}
public static void main(String[] args) {
test(null);
}
}
OUTPUT
Exception in thread "main" java.lang.NullPointerException
at ExceptionTest.test(ExceptionTest.java:6)
at ExceptionTest.main(ExceptionTest.java:15)
----------------------------------------------------------------------
NumberFormatException
public class ExceptionTest {
public static void main(String[] args) {
try{
String s="ramesh";
int i=Integer.parseInt(s);
}catch(NumberFormatException e)
{
throw new NumberFormatException("NumberFormatException is thrown");
}
}
}
output
Exception in thread "main" java.lang.NumberFormatException: NumberFormatException is thrown
at ExceptionTest.main(ExceptionTest.java:11)
-------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
public class ExceptionTest {
public static void main(String[] args) {
try{
int[] arr=new int[-5];
}catch(NegativeArraySizeException e)
{
System.out.println("negative arraysize exception");
}
}
}
output
negative arraysize exception
---------------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ExceptionTest {
public static final String file="input.txt";
public static void main(String[] args) {
try{
BufferedReader reader=new BufferedReader(new FileReader(file));
System.out.println("next line");
}catch(FileNotFoundException e)
{
System.out.println("filenotfoundexception caught");
}
}
}
output
filenotfoundexception caught
----------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.