In: Computer Science
This programming assignment involves learning about some common exceptions which occur in Java programs. Consider the following exception types:
NullPointerException
ArrayIndexOutOfBounds
Exception ClassCastException
IllegalArgumentException
Research what each exception type means and the conditions under which each occurs (thrown). Then write the following programs, one for each of the above-listed exception types:
A program which throws the exception (with a throw statement) and catches it displaying unique information about the exception. Name your programs <exception>Thrown.java
<exception> is the name of the exception involved for example NullPointerExceptionThrown.java.
A program which causes the exception to be thrown (not with a throw statement) and catches it displaying unique information about the exception (ex. Name of class and method causing the exception). For example, for the NullPointerException, have your program create a situation which would cause this exception to be thrown. Name your programs <exception>Catch.java <exception> is the name of the exception involved for example NullPointerExceptionCatch.java.
At the end you should have eight programs, four <exception>Thrown.java and four <exception>Catch.java. All files should be in the same directory.
1st file
NullPointerExceptions are exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object.
NullPointerExceptionThrown.java
public class NullPointerExceptionThrown {
public static void hasCode(){
System.out.println("Do
Something");
}
public static void main(String[] args) {
Object a = null;
a.hashCode();
}
}
Output:
2nd File:
NullPointerExceptionCatch.java
public class NullPointerExceptionCatch {
public static void hasCode(){
System.out.println("Do
Something");
}
public static void main(String[] args) {
try {
Object a = null;
a.hashCode();
} catch(NullPointerException e) {
System.out.println("NullPointerException.. Handled");
}
}
}
3rd File
If a request for a negative or an index greater than or equal to size of array is made, then the JAVA throws a ArrayIndexOutOfBounds Exception.
ArrayIndexOutOfBoundThrown.java
public class ArrayIndexOutOfBoundThrown{
public static void main(String[] args) {
int[] arr=new int[5];
int i=0;
System.out.println(arr[6]);
}
}
4th File
ArrayIndexOutOfBoundCatch.java
public class ArrayIndexOutOfBoundCatch{
public static void main(String[] args) {
try{
int[] arr=new int[5];
int i=0;
System.out.println(arr[6]);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
}
}
5th File
if you are trying to typecast an object of class A into an object of class B, and they aren't compatible, you get a class cast exception.
classCastExceptionThrown.java
public class classCastExceptionThrown {
public static void main(String[] args) {
Object i = Integer.valueOf(42);
String s = (String)i;
}
}
6th File
classCastExceptionCatch.java
public class classCastExceptionThrown {
public static void main(String[] args) {
try{
Object i =
Integer.valueOf(42);
String s = (String)i;
} catch(ClassCastException e)
{
System.out.println("ClassCastException.. Handled");
}
}
}
7th File
The IllegalArgumentException is a good way of handling possible errors in your application’s code. This exception indicates that a method is called with incorrect input arguments.
IllegalArgumentExceptionThrown.java
import java.io.File;
public class IllegalArgumentExceptionThrown {
public static String createRelativePath(String parent, String
filename) {
if(parent == null)
throw new IllegalArgumentException("The parent path cannot be
null!");
if(filename == null)
throw new IllegalArgumentException("The filename cannot be
null!");
return parent + File.separator + filename;
}
public static void main(String[] args) {
// The following command will be successfully executed.
System.out.println(IllegalArgumentExceptionThrown.createRelativePath("dir1",
"file1"));
System.out.println();
// The following command throws an IllegalArgumentException.
System.out.println(IllegalArgumentExceptionThrown.createRelativePath(null,
"file1"));
}
}
}
8th File
IllegalArgumentExceptionCatch.java
import java.io.File;
public class IllegalArgumentExceptionCatch {
public static String createRelativePath(String parent, String
filename) {
if(parent == null)
throw new IllegalArgumentException("The parent path cannot be
null!");
if(filename == null)
throw new IllegalArgumentException("The filename cannot be
null!");
return parent + File.separator + filename;
}
public static void main(String[] args) {
// The following command will be successfully executed.
try{
System.out.println(IllegalArgumentExceptionCatch.createRelativePath("dir1",
"file1"));
System.out.println();
// The following command throws an IllegalArgumentException.
System.out.println(IllegalArgumentExceptionCatch.createRelativePath(null,
"file1"));
} catch (IllegalArgumentException e){
System.out.println("IllegalArgumentException Handled !!");
}
}
}