Hey girls and guys,
I can't find the problem here. There are 10 exceptions in my list. According to the recommendation (in the title) I seem to invoke an unhealthy exception. Does anybody know which it could be?
best regards
Mathis
package com.codegym.task.task14.task1419;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/*
Exception invasion
Populate the list exceptions with ten (10) different exceptions.
The first exception is already implemented in the initExceptions method.
Requirements:
1. The list exceptions must contain 10 elements.
2. All items in the list exceptions must be exceptions (descendants of the Throwable class).
3. All items in the list exceptions must be unique.
4. The initExceptions method must be static.
*/
public class Solution {
public static List<Exception> exceptions = new ArrayList<Exception>();
public static void main(String[] args) {
initExceptions();
for (Exception exception : exceptions) {
System.out.println(exception);
}
}
private static void initExceptions() { // The first exception
//1
try {
float i = 1 / 0;
} catch (ArithmeticException e) {
exceptions.add(e);
}
//2
try {
Object object = null;
object.toString();
} catch (NullPointerException e) {
exceptions.add(e);
}
//3
try {
String s = "asdasd";
int i = Integer.parseInt(s);
} catch (NumberFormatException e) {
exceptions.add(e);
}
//4
try {
Object i = Integer.valueOf(42);
String s = (String)i;
} catch (ClassCastException e) {
exceptions.add(e);
}
//5
try {
String[] dateStr = new String[-5];
} catch (NegativeArraySizeException e) {
exceptions.add(e);
}
//6
try {
new FileReader(new File("/invalid/file/location"));
} catch (FileNotFoundException e) {
exceptions.add(e);
}
//7
try {
new URL("malformedurl");
} catch (MalformedURLException e) {
exceptions.add(e);
}
//8
try {
int[] nums = new int[] {1, 2, 3};
int numFromNegativeIndex = nums[-1];
} catch (ArrayIndexOutOfBoundsException e) {
exceptions.add(e);
}
//9
try {
String str = "Hello World";
char charAtNegativeIndex = str.charAt(-1);
} catch (StringIndexOutOfBoundsException e) {
exceptions.add(e);
}
//10
try {
Object x[] = new String[3];
x[0] = new Integer(0);
} catch (ArrayStoreException e) {
exceptions.add(e);
}
}
}