This programming assignment involves learning about some common exceptions which
ID: 3808278 • Letter: T
Question
This programming assignment involves learning about some common exceptions which occur in Java programs. Consider the following exception types: Null Pointer Exception Array IndexoutOfBoundsException 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 information about the exception. Name your programs Thrown.java is the name of the exception involved for example NullpointerExceptionThrown.java. A program which causes the exception to be thrown (nor 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 Catch.java is the name of the exception involved for example NullPointerExceptionCatch.java. At the end you should have eight programs, four Thrown.java and four Catch.java. Submit the source code in a zip file named as follows: Assignment9_ followed by your first name initial, followed by your last name, followed by your course section number. For example, if your name is Jane Smith and you are in section 81, your zip file's name would be Assignment9Jsmith81.zi. Please also state how to compile from command prompt.Explanation / Answer
//1============== NullPointerExceptionThrown.java ==============//
public class NullPointerExceptionThrown {
public static void main(String[] args) {
String str = "test";
System.out.println("-----calling with str="test" ------");
checkNullPointerException(str);
str = null;
System.out.println("-----calling with str=null -------");
checkNullPointerException(str);
}
public static void checkNullPointerException( String str){
try{
if(str==null)
throw new java.lang.NullPointerException();
else
System.out.print("No exception ");
}catch(java.lang.NullPointerException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
//2============== ArrayIndexOutOfBoundsExceptionThrown.java ==============//
public class ArrayIndexOutOfBoundsExceptionThrown {
public static int[] array = new int[4];
public static void main(String[] args) {
int index = 3;
System.out.println("------calling with index=3");
checkArrayIndexOutOfBoundsException(index);
index=10;
System.out.println("------calling with index=10");
checkArrayIndexOutOfBoundsException(index);
}
public static void checkArrayIndexOutOfBoundsException(int index){
try{
if(index>array.length-1){
throw new ArrayIndexOutOfBoundsException();
}
else{
System.out.println("This is currect indext");
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
//3============== ClassCastExceptionThrown.java ==============//
public class ClassCastExceptionThrown {
public static void main(String[] args) {
Object obj = new String("test");
System.out.println("-----calling with obj=new String ------");
checkClassCastException(obj);
obj = new Integer(4);
System.out.println("-----calling with obj=new Integer -------");
checkClassCastException(obj);
}
public static void checkClassCastException(Object obj){
try{
if(obj instanceof String){
System.out.println("No exception");
}
else
throw new ClassCastException();
}
catch(ClassCastException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
//4============== IllegalArgumentExceptionThrown.java ==============//
public class IllegalArgumentExceptionThrown {
public static void main(String[] args) {
int value = 6;
System.out.println("-------calling with value=6 ---------");
checkIllegalArgumentException(value);
value = 100;
System.out.println("-------calling with value=100 ---------");
checkIllegalArgumentException(value);
}
public static void checkIllegalArgumentException(int value){
try{
if(value<0 || value >10){
throw new IllegalArgumentException();
}
else{
System.out.println("No exception");
}
}catch(IllegalArgumentException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
//5============== NullPointerExceptionCatch.java ==============//
public class NullPointerExceptionCatch {
public static void main(String[] args) {
String str = "test";
System.out.println("-----calling with str="test" ------");
checkNullPointerException(str);
str = null;
System.out.println("-----calling with str=null -------");
checkNullPointerException(str);
}
public static void checkNullPointerException( String str){
try{
str.charAt(2);
System.out.print("No exception ");
}catch(java.lang.NullPointerException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
//6============== ArrayIndexOutOfBoundsExceptionCatch .java ==============//
public class ArrayIndexOutOfBoundsExceptionCatch {
public static int[] array = new int[4];
public static void main(String[] args) {
int index = 3;
System.out.println("------calling with index=3");
checkArrayIndexOutOfBoundsException(index);
index=10;
System.out.println("------calling with index=10");
checkArrayIndexOutOfBoundsException(index);
}
public static void checkArrayIndexOutOfBoundsException(int index){
try{
array[index]=50;
System.out.println("This is currect indext");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
//7=======================ClassCastExceptionCatch.java ==============//
public class ClassCastExceptionCatch {
public static void main(String[] args) {
Object obj = new String("test");
System.out.println("-----calling with obj=new String ------");
checkClassCastException(obj);
obj = new Integer(4);
System.out.println("-----calling with obj=new Integer -------");
checkClassCastException(obj);
}
public static void checkClassCastException(Object obj){
try{
String str = (String)obj;
System.out.println("No exception");
}
catch(ClassCastException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
//8=======================IllegalArgumentExceptionCatch.java ==============//
public class IllegalArgumentExceptionCatch {
public static void main(String[] args) {
int value = 6;
System.out.println("-------calling with value=6 ---------");
checkIllegalArgumentException(value);
value = 100;
System.out.println("-------calling with value=100 ---------");
checkIllegalArgumentException(value);
}
public static void checkIllegalArgumentException(int value){
try{
Value val = new Value(value);
System.out.println("No exception");
}catch(IllegalArgumentException e){
System.out.println("Exception catched ");
System.out.println("LocalizedMessag :"+e.getLocalizedMessage());
System.out.println("toString :"+e.toString());
System.out.println("Messag :"+e.getMessage());
System.out.println("StackTrace :");
//e.printStackTrace();
}
}
}
class Value{
public Value(int val){
if(val<0 || val >10)
throw new IllegalArgumentException();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.