In: Computer Science
This programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception) and provide a screen capture of the output. You should write your code so that each exception type is forced to occur. Name your programs as follows: NullPointerExceptionThrown, etc. Then, write a program that catches each type of thrown exception and display an error message indicating some of the details as to what that exception is. Write one program per type of exception. Name your programs as follows: NullPointerExceptionCatch, etc.
The problem of accessing an array element beyond the size of an array is represented by a class called ArrayIndexOutOfBoundsException. Let’s take an example- suppose marks of 20 students are stored in an array. A particular student marks are known by taking the roll number (it is index number) from the keyboard. The array index is from 0 to 19. If the user enters 20, there is no element in an array with index 20. JVM terminates the execution. Before terminating, it gives a proper message of the problem. This example is like this mentioned example. The index is permitted 0 to 2 only but the user is giving 3, it is beyond the array size.
import java.io.*;
public class ArrayIndexOutofBoundThrown
{
public static void main(String args[])
{
int marks[] = { 40, 50, 60 };
System.out.println("Hello 1");
int m1 = marks[3];
System.out.println("Marks are " + m1);
System.out.println("Hello 2");
System.out.println("Hello 3");
}
}
NullPointerException indicates, if an object points to null and further used in the code, the JVM throws NullPointerException. In the following program, string str is assigned with Null. The Null variable str is compared with “Hello”.The JVM reacts by throwing NullPointerException.
import java.io.*;
public class NullPointerExceptionThrown
{
public static void main(String args[])
{
String str = null;
System.out.println(str.equals("hello"));
}
}
As the name indicates, this exception is raised by JVM when it is unable to cast two objects which are done in the program by the programmer against the rules of java casting. In this program, test object f is explicitly cast to ClassCastExceptionThrow and assigned. It compiles, but at execution time throws ClassCastException. Why did it raise? The rule says “before doing explicit casting, there must be implicit casting done earlier”. But in this code, it is not done.
import java.io.*;
class test {
}
public class ClassCastExceptionThrown extends test
{
public static void main(String args[])
{
test f = new test();;
ClassCastExceptionThrown r = new ClassCastExceptionThrown();
// f = r;
r = (ClassCastExceptionThrown) f;
}
}
It is thrown by Color constructor when wrong parameters are passed. The RGB values should be within the range of 0 to 255. If other values are passed Color constructor throws IllegalArgumentException.
import java.awt.*;
public class IllegalArgumentExceptionThrown
{
public static void main(String args[])
{
Color clr1 = new Color(300, 150, 200);
}
}