Consider the following exception types: NullPointerException ArrayIndexOutOfBoun
ID: 3683595 • Letter: C
Question
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. 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.
Explanation / Answer
NullPointerException:
Null Pointer Exception occurs when Null reference is used in place where the requirement is for an object reference.
NullPointerException1.java
public class NullPointerException1 {
/**
* @param args
*/
private static String name;
public static void main(String[] args) {
// TODO Auto-generated method stub
if(name.equals("Java")){
System.err.println("Welcome to Java");
}
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
at com.examples.NullPointerException.main(NullPointerException.java:11)
NullPointerExceptionCatch.java
public class NullPointerExceptionCatch {
/**
* @param args
*/
private static String name;
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
if(name.equals("Java")){
System.err.println("Welcome to Java");
}
}
catch(NullPointerException e){
System.out.println("Name value is null");
}
}
}
Output:
Name value is null
ArrayIndexOutOfBoundsException1.java
public class ArrayIndexOutOfBoundsException1 {
/**
* @param args
*/
public static void main(String args[])
{
int marks[] = { 40, 50, 60 };
System.out.println("Hello 1");
int m1 = marks[3]; // permitted index numbers are 0, 1 and 2
System.out.println("Marks are " + m1);
System.out.println("Hello 2");
System.out.println("Hello 3");
}
}
Output:
Hello 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at com.examples.ArrayIndexOutOfBoundsException.main(ArrayIndexOutOfBoundsException.java:13)
ArrayIndexOutOfBoundsExceptionCatch.java
public class ArrayIndexOutOfBoundsExceptionCatch {
/**
* @param args
*/
public static void main(String args[])
{
try{
int marks[] = { 40, 50, 60 };
System.out.println("Hello 1");
int m1 = marks[3]; // permitted index numbers are 0, 1 and 2
System.out.println("Marks are " + m1);
System.out.println("Hello 2");
System.out.println("Hello 3");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index out of Bunds Exception occurs.." );
}
System.out.println("Hello 2");
System.out.println("Hello 3");
}
}
Output:
Hello 1
Array Index out of Bunds Exception occurs..
Hello 2
Hello 3
ClassCastException1.java
public class ClassCastException1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Object obj = new Integer(100);
System.out.println((String) obj);
}
}
Output:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at com.examples.ClassCastException.main(ClassCastException.java:11)
ClassCastExceptionCatch.java
public class ClassCastExceptionCatch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Object obj = new Integer(100);
System.out.println((String) obj);
}catch(ClassCastException e){
System.out.println("Class Cast Exception Occurs");
}
}
}
Output:
Class Cast Exception Occurs
IllegalArgumentException1.java
public class IllegalArgumentException1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Percentage percentage = new Percentage(121);
System.out.println(percentage.getValue());
}
}
class Percentage {
private final int value;
/**
* A percentage value must be between 0 and 100 inclusive.
*
* @param value
* the percentage value
*/
public Percentage(int value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException(Integer.toString(value));
}
this.value = value;
}
public int getValue() {
return value;
}
}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: 121
at com.examples.Percentage.<init>(IllegalArgumentException1.java:27)
at com.examples.IllegalArgumentException1.main(IllegalArgumentException1.java:10)
IllegalArgumentExceptionCatch.java
public class IllegalArgumentExceptionCacth {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Percentage percentage = new Percentage(121);
System.out.println(percentage.getValue());
}
catch(IllegalArgumentException e){
System.out.println("Illegal Argument Exception Occurs");
}
}
}
class Percentage {
private final int value;
/**
* A percentage value must be between 0 and 100 inclusive.
*
* @param value
* the percentage value
*/
public Percentage(int value) {
if (value < 0 || value > 100) {
throw new IllegalArgumentException(Integer.toString(value));
}
this.value = value;
}
public int getValue() {
return value;
}
}
Output:
Illegal Argument Exception Occurs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.