COLLEGE OF COMPUTER STUDEIS
INFORMATION TECHNOLOGY DEPARTMENT
CCS0023L
(Object Oriented Programming)
Machine Problem
8
How to implement exception and assertion
Rollen Rivera.
Student Name:
AB21
Section:
Sir. Mark Sabili
Professor:
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.
public class Main {
public static void main(String[] args) {
String[] strings = {"a", "b", "c", null, "d"};
try {
for(int i=0; i<strings.length+1; i++) {
System.out.println(strings[i].toUpperCase ());
}
System.out.println(strings[10]);
}
catch(NullPointerException ne) {
System.out.println("Null pointer exception caught.");
}
catch(ArrayIndexOutOfBoundsException ae) {
System.out.println("Array idex out of bounds exception caught.");
}
catch(RuntimeException re) {
System.out.println("Runtime exception caught");
}
}