I have a method which adds two vectors together and I need to return an exception if the length of these vectors are not the same to start off with. I have written a code
public static Vector vectorAdd(Vector v1, Vector v2) throws IllegalOperandException{
if(v1.getLength() == v2.getLength()) {
double[] temp = new double[v1.getLength()];
for(int i = 0; i < temp.length; i++) {
temp[i] = v1.get(i) + v2.get(i);
}
Vector v3 = new Vector(temp);
return v3;
} else {
throw new IllegalOperandException("Length of Vectors Differ");
}
}
But once I compile my main method
else if (userInput == 2) {
System.out.println("Please enter a vector!");
System.out.println("Separate vector components by "
+ "using a space.");
Vector v1 = input.readVector();
System.out.println();
System.out.println("Please enter a vector!");
System.out.println("Separate vector components by "
+ "using a space.");
Vector v2 = input.readVector();
System.out.println();
System.out.println();
System.out.println(LinearAlgebra.VectorAdd(v1, v2));
There is an error of
error: unreported exception IllegalOperandException; must be caught or declared to be thrown System.out.println(LinearAlgebra.vectorAdd(v1, v2));
I have been googling for an hour now, but I do not get what is the problem. I'm pretty sure it's something related with try and catch, but I have no idea how to fix it. What should I do?