Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Provide command prompt instructions This programming assignment involves learnin

ID: 3809262 • Letter: P

Question

Provide command prompt instructions

This programming assignment involves learning about some common exceptions which occur in Java programs. Consider the following exception types: 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 (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 Null Pointer Exception, 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 Assignment9_jsmith81.zip. Please also state how to compile from command prompt.

Explanation / Answer

NullPointerExceptionThrown.java

public class NullPointerExceptionThrown {
public static void throwsNullPointerException()
{
throw new NullPointerException("Null pointer exception example");
}
  
public static void main(String[] args)
{
try
{
throwsNullPointerException();
}
catch (NullPointerException e){
System.out.println("Null pointer exception throws. Message: " + e.getMessage());
}
}

}

ArrayIndexOutOfBoundExceptionThrown.java


public class ArrayIndexOutOfBoundExceptionThrown {
public static void throwsArrayIndexOutOfBoundException()
{
throw new ArrayIndexOutOfBoundsException("Array out of bound exception.");
}
  
public static void main(String[] args)
{
try
{
throwsArrayIndexOutOfBoundException();
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
}

}

ClassCastExceptionThrown.java


public class ClassCastExceptionThrown {
public static void throwsClassCastException()
{
throw new ClassCastException("Class Cast exception.");
}
  
public static void main(String[] args)
{
try
{
throwsClassCastException();
}
catch (ClassCastException e){
System.out.println(e.getMessage());
}
}

}

IllegalArgumentExceptionThrown.java


public class IllegalArgumentExceptionThrown {
public static void throwsIllegalArgumentException()
{
throw new IllegalArgumentException("Illegal argument exception.");
}
  
public static void main(String[] args)
{
try
{
throwsIllegalArgumentException();
}
catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}

}

NullPointerExceptionCatch.java


public class NullPointerExceptionCatch {
public static String throwsNullPointerException()
{
Object a = null;
return a.toString();
  
}
  
public static void main(String[] args)
{
try
{
throwsNullPointerException();
}
catch (NullPointerException e){
System.out.println("Null point exception occured in NullPointerExceptionCatch class in method throwsNullPointerException");
}
}

}

ArrayIndexOutOfBoundsExceptionCatch.java


public class ArrayIndexOutOfBoundsExceptionCatch {
public static int throwsArrayIndexOutOfBoundsException()
{
int[] a = new int [5];
return a[5];
  
}
  
public static void main(String[] args)
{
try
{
throwsArrayIndexOutOfBoundsException();
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("Array index out of bound exception occured in ArrayIndexOutOfBoundsExceptionCatch class in method throwsArrayIndexOutOfBoundsException");
}
}

}

ClassCastExceptionCatch.java


public class ClassCastExceptionCatch {
public static void throwsClassCastException()
{
Object i = Integer.valueOf(42);
String s = (String)i;
}
  
public static void main(String[] args)
{
try
{
throwsClassCastException();
}
catch (ClassCastException e){
System.out.println("Class cast exception occured in ClassCastExceptionCatch class in method throwsClassCastException");
}
}

}

IllegalArgumentExceptionCatch.java


public class IllegalArgumentExceptionCatch {
public static void throwsIllegalArgumentException()
{
Character.toChars(-1);
  
}
  
public static void main(String[] args)
{
try
{
throwsIllegalArgumentException();
}
catch (IllegalArgumentException e){
System.out.println("Illegal argument exception occured in IllegalArgumentExceptionCatch class in method throwsIllegalArgumentException");
}
}

}

$ javac *Catch.java *Thrown.java

$ java NullPointerExceptionCatch
Null point exception occured in NullPointerExceptionCatch class in method throwsNullPointerException
$ java NullPointerExceptionThrown
Null pointer exception throws. Message: Null pointer exception example

$ java ArrayIndexOutOfBoundsExceptionCatch
Array index out of bound exception occured in ArrayIndexOutOfBoundsExceptionCatch class in method throwsArrayIndexOutOfBoundsExceptio
$ java ArrayIndexOutOfBoundsExceptionThrown
Array out of bound exception.

$ java ClassCastExceptionCatch
Class cast exception occured in ClassCastExceptionCatch class in method throwsClassCastException
$ java ClassCastExceptionThrown
Class Cast exception.

$ java IllegalArgumentExceptionCatch
Illegal argument exception occured in IllegalArgumentExceptionCatch class in method throwsIllegalArgumentException
$ java IllegalArgumentExceptionThrown
Illegal argument exception.
$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote