Java 未报告的异常;必须被抓住或宣布被抛出

Java 未报告的异常;必须被抓住或宣布被抛出,java,class,exception,Java,Class,Exception,我有两个文件: NewtonRaphson public class NewtonRaphson { public double squareRoot ( double input ) throws NegativeNumber{ if (input < 0.0 ) { throw new NegativeNumber("not allowed to input a negative number");} else if (input ==

我有两个文件:

NewtonRaphson

public class NewtonRaphson {
    public double squareRoot  ( double input ) throws   NegativeNumber{
        if (input < 0.0 ) { throw new   NegativeNumber("not allowed to input a negative number");}
        else if (input == 0.0) { return 0.0; }
        else{
            double current = 10.0;
            for (int i = 0; i < 10; ++i) {
                current = current - (current*current - input)/(2*current);
            }
            return current;
        }
    }

    public static void main(String[] args){
        NewtonRaphson nr = new NewtonRaphson();
        System.out.println(nr.squareRoot(2.0));

    }
}
当我编译第一个时,我得到:

NewtonRaphson.java:17: unreported exception NegativeNumber; must be caught or declared to be thrown
    System.out.println(nr.squareRoot(2.0));
                                    ^
1错误


有人能帮我解释一下原因吗?

你扔了
NegativeNumber
,但一直没能抓住它。这是什么棒球

main
方法中添加一个try-catch

public static void main(String[] args){
    NewtonRaphson nr = new NewtonRaphson();

    try {
       System.out.println(nr.squareRoot(2.0));
    } catch (NegativeNumber e) {
       System.out.println("Be more positive!");
    }
}
或者,如果你永远不想抓住它(不建议这样做):


您正在抛出
negativeEnumber
,但从未捕获它。这是什么棒球

main
方法中添加一个try-catch

public static void main(String[] args){
    NewtonRaphson nr = new NewtonRaphson();

    try {
       System.out.println(nr.squareRoot(2.0));
    } catch (NegativeNumber e) {
       System.out.println("Be more positive!");
    }
}
或者,如果你永远不想抓住它(不建议这样做):


您正在抛出
negativeEnumber
,但从未捕获它。这是什么棒球

main
方法中添加一个try-catch

public static void main(String[] args){
    NewtonRaphson nr = new NewtonRaphson();

    try {
       System.out.println(nr.squareRoot(2.0));
    } catch (NegativeNumber e) {
       System.out.println("Be more positive!");
    }
}
或者,如果你永远不想抓住它(不建议这样做):


您正在抛出
negativeEnumber
,但从未捕获它。这是什么棒球

main
方法中添加一个try-catch

public static void main(String[] args){
    NewtonRaphson nr = new NewtonRaphson();

    try {
       System.out.println(nr.squareRoot(2.0));
    } catch (NegativeNumber e) {
       System.out.println("Be more positive!");
    }
}
或者,如果你永远不想抓住它(不建议这样做):


好的,但是您现在正在使用内置的NegativeEnumberException。但是如果我想使用我自己声明的异常,如果输入恰好是负数,该怎么办?@user2761810:那么您只需更改名称。您的命名方案令人困惑,异常在逻辑上应该以“Exception”结尾。就像其他例外一样。为什么要重新创建与现有异常相同的异常?好的,但您现在正在使用内置的NegativeEnumberException。但是如果我想使用我自己声明的异常,如果输入恰好是负数,该怎么办?@user2761810:那么您只需更改名称。您的命名方案令人困惑,异常在逻辑上应该以“Exception”结尾。就像其他例外一样。为什么要重新创建与现有异常相同的异常?好的,但您现在正在使用内置的NegativeEnumberException。但是如果我想使用我自己声明的异常,如果输入恰好是负数,该怎么办?@user2761810:那么您只需更改名称。您的命名方案令人困惑,异常在逻辑上应该以“Exception”结尾。就像其他例外一样。为什么要重新创建与现有异常相同的异常?好的,但您现在正在使用内置的NegativeEnumberException。但是如果我想使用我自己声明的异常,如果输入恰好是负数,该怎么办?@user2761810:那么您只需更改名称。您的命名方案令人困惑,异常在逻辑上应该以“Exception”结尾。就像其他例外一样。为什么要重新创建一个与现有异常相同的异常?我会将其更改为RuntimeException,这样就不必捕获它。我会将其更改为RuntimeException,这样就不必捕获它。我会将其更改为RuntimeException,这样就不必捕获它。我会将其更改为RuntimeException,然后将其删除不必被抓住。