简体   繁体   中英

Java unreported Exception error

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM