1

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?

  • 2
    you have throw exception IllegalOperandException.so you need to handle exception – Madhawa Priyashantha Oct 16 '14 at 21:53
  • Try surrounding that System.out.println(LinearAlgebra blah blah) with a try/catch block that specifically handles IllegalOperandException or generically, handles Exception. – MarsAtomic Oct 16 '14 at 21:53
  • 1
    @MarsAtomic http://stackoverflow.com/questions/2416316/why-is-the-catchexception-almost-always-a-bad-idea – m0skit0 Oct 16 '14 at 21:56
  • This may be useful: [What are checked exceptions?](http://stackoverflow.com/questions/9371686/what-are-checked-exceptions-in-java-c) – clcto Oct 16 '14 at 21:57
  • @m0skit0 Good point. I was thinking about a temporary way around the issue while the code was being crafted, but it's never too early to exhibit good form. – MarsAtomic Oct 17 '14 at 19:03

1 Answers1

3

Whenever you do something that can throw a particular type of Exception, you have to have something in place to deal with it. This can be either of two things:

  1. surround it with a try/catch block;
  2. add the Exception type to the throws clause of your method.

In your case, you're invoking the LinearAlgebra.vectorAdd() method, and that method can throw an IllegalOperandException (presumably if one of its arguments is dodgy). That means that the method in which you invoke it can also throw that exception. Either catch it, or add throws IllegalOperandException to the signature of the method in which that line occurs. Sounds as though it's your main method, so it would become

public static void main(String[] args) throws IllegalOperandException {
    //...
}

This is called letting the exception propagate upwards.

To catch the exception instead, you'd have

try {
    System.out.println(LinearAlgebra.VectorAdd(v1, v2));
} catch (IllegalOperandException e) {
    // do something with the exception, for instance:
    e.printStackTrace();
    // maybe do something to log it to a file, or whatever...
    // or you might be able to recover gracefully...
    // or if there's just nothing you can do about it, then you might:
    System.exit(1);
}

That will allow you to deal with it when it happens. It enables you to return some specific result instead if it all goes wrong, or (in this case) print an error and terminate the program.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67