15. public class ExceptionExample5 {
public static void main(String[] args) {
int arrayOfInt[] = {0, 1, 2};
for(int index=1; index <= arrayOfInt.length; index++) {
try {
System.out.println(arrayOfInt[index]);
throw new MySecondException("No array index out of bounds
exception thrown yet!");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("nIndex not in range!");
}
catch (MySecondException e) {
System.out.println("Second Exception Thrown:");
System.out.println(e.getClass());
System.out.println(e.getMessage());
}
finally {
System.out.println("This will print anyway!");
}
}
}
}
:הפלט שיתקבל כך
1
Second Exception Thrown:
class MySecondException
No Exception: No array index out of bounds exception thrown yet!
This will print anyway!
2
Second Exception Thrown:
class MySecondException
No Exception: No array index out of bounds exception thrown yet!
This will print anyway!
Index not in range!
This will print anyway!