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

605.201 Introduction to Programming Using Java Assignment 9 This programming ass

ID: 3604375 • Letter: 6

Question

605.201 Introduction to Programming Using Java Assignment 9 This programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NullPointerException ArraylndexOutOfBoundsException 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: NullPointerException Thrown, 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

1. NullPointerException:

- It is a runtime exception

- This exception occurs when you declare a variable but did not create any object.

- In such cases, null value is assigned to an object reference.

- When program attempts to use an object reference that has the null value, NullPointerException is thrown.

2. ArrayIndexOutOfBoundsException:

- ArrayIndexOutOfBoundsException is thrown if requested array index is either:

i. negative or

ii. greater than or equal to size of array

3. ClassCastException:

- ClassCastException is thrown to indicate that we have attempted to cast an object to a subclass, of which it is not an instance.

4. IllegalArgumentException:

- When illegal arguments are passed to method, IllegalArgumentException is thrown.

Examples -

1. NullPointerException:

NullPointerExceptionThrown.java

public class NullPointerExceptionThrown{

public static void main(String[] args) {

// TODO Auto-generated method stub

// Initializing String variable with null value

String pointer = null;

try

{

if (pointer.equals("gfg"))

System.out.print("Same");

else

System.out.print("Not Same");

}

catch(NullPointerException e)

{

System.out.print("NullPointerException Caught");

}

}

}

Output: NullPointerException Caught

2. ArrayIndexOutOfBoundsException:

ArrayIndexOutOfBoundsExceptionThrown.java

public class ArrayIndexOutOfBoundsExceptionThrown {

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] values={1,2,3,4,5};

try

{

for(int i=0;i<values.length+1;i++)

{

System.out.println(values[i]);

}

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("Array Index Out Of Bounds Exception caught!");

}

}

}

Output:

1
2
3
4
5
Array Index Out Of Bounds Exception caught!

3. Class Cast Exception:

ClassCastExceptionThrown.java

public class ClassCastExceptionThrown {

public static void main(String[] args) {

// TODO Auto-generated method stub

try

{

Object i = Integer.valueOf(50);

String s = (String)i;  

}

catch(ClassCastException e)

{

System.out.println("Class Cast Exception thrown!");

}

}

}

Output:

Class Cast Exception thrown!

4. IllegalArgumentException:

IllegalArgumentException.java

public class IllegalArgumentException extends RuntimeException{

public static void main(String[] args) {

// TODO Auto-generated method stub

String val1 = null;

try

{

System.out.println(getLen(val1));

}

catch(IllegalArgumentException e)

{

System.out.println("IllegalArgumentException caught");

}

}

public static int getLen(String val)

{

if (val == null)

throw new IllegalArgumentException();

return val.length();

}

}

Output:

IllegalArgumentException caught

*** Hope this helps! :)