(Object Oriented Programming) : CCS0023L
(Object Oriented Programming) : CCS0023L
CCS0023L
(Object Oriented Programming)
Machine Problem
8
How to implement exception and assertion
I.
Create a Class FindArray that will try to iterate in the array displaying the content
in each index. The program should be able to catch an
ArrayIndexOutOfBoundsException, a NullPointerException and a
RunTimeException.
NullPointerException
package com.company;
try {
int[] a = null;
System.out.println(a[3]);
}
catch(NullPointerException e)
{
System.out.println("The exception is " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The exception is " + e);
}
catch(RuntimeException e)
{
System.out.println("The exception is " + e);
}
}
}
SAMPLE OUTPUTS(NullPointerException)
ArrayIndexOutOfBoundsException
package com.company;
try {
int[] a = {1,2,3};
System.out.println(a[3]);
}
catch(NullPointerException e)
{
System.out.println("The exception is " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The exception is " + e);
}
catch(RuntimeException e)
{
System.out.println("The exception is " + e);
}
}
}
SAMPLE OUTPUTS(ArrayIndexOutOfBoundsException)
RunTimeException
package com.company;
try {
throw new RuntimeException("Hello");
}
catch(NullPointerException e)
{
System.out.println("The exception is " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("The exception is " + e);
}
catch(RuntimeException e)
{
System.out.println("The exception is " + e);
}
}
}
SAMPLE OUTPUTS(RunTimeException)