简体   繁体   English

Java未报告的异常错误

[英]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; 错误:未报告的异常IllegalOperandException; must be caught or declared to be thrown System.out.println(LinearAlgebra.vectorAdd(v1, v2)); 必须被捕获或声明被抛出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. 我很确定它与try和catch相关,但我不知道如何修复它。 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. 每当你做一些可以抛出特定类型的Exception东西时,你必须有适当的东西来处理它。 This can be either of two things: 这可以是两件事之一:

  1. surround it with a try / catch block; try / catch块包围它;
  2. add the Exception type to the throws clause of your method. Exception类型添加到方法的throws子句中。

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). 在你的情况下,你正在调用LinearAlgebra.vectorAdd()方法,并且该方法可以抛出IllegalOperandException (可能是因为它的一个参数是狡猾的)。 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. 捕获它,或者将throws IllegalOperandException到该行发生的方法的签名中。 Sounds as though it's your main method, so it would become 听起来好像这是你的main方法,所以它会成为

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. 如果一切都出错,它可以让你返回一些特定的结果,或者(在这种情况下)打印错误并终止程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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