Java newbie here and I'm doing an exercise trying to catch thrown exceptions by using as few try...catch statements as possible. When I compiled the code, I received IllegalArgumentException
errors and wasn't sure how to fix this by using the try...catch statements.
I've watched tutorials, looked at other examples but since this is a new concept for me, I'm still unsure how to use the try...catch for this particular exercise.
public class Main extends Object {
public static void main(String [] args) {
tryGetMax();
tryRemove();
private static final void tryGetMax() {
int max = 0;
max = FunMethods.getMax((Integer[])null);
Integer[] numbers = new Integer[50];
Random rand = new Random();
for (int i = 0; i < 50; i++) {
numbers[i] = new Integer(rand.nextInt(500));
}
numbers[32] = null;
max = FunMethods.getMax(numbers);
numbers[32] = new Integer(rand.nextInt(500));
max = FunMethods.getMax(numbers);
}
The second part of the exercise:
private static final void tryRemove() {
FunMethods.remove(null, 2);
Object[] someObjects = new Object[12];
someObjects[0] = "a string!";
someObjects[1] = new Integer(32);
someObjects[2] = new Float(42.5f);
someObjects[3] = "another string";
for (int i = 4; i < someObjects.length; i++) {
someObjects[i] = String.valueOf(i);
}
FunMethods.remove(someObjects, 12);
someObjects = FunMethods.remove(someObjects, 3);