【问题标题】:Handling throws with an ArithmaticException Constructor使用 ArithmaticException 构造函数处理抛出
【发布时间】:2015-08-08 23:28:54
【问题描述】:

问题: 编写一个名为 addTwoPositive 的方法,它接受两个 int 参数,如果其中一个值不是正数,则抛出一个 ArithmeticException,传递字符串“已发送非正整数!”到异常的构造函数。如果两个值都是正数,则返回它们的总和。

我对如何通过ArithmeticException 构造函数处理此异常感到困惑。这是我的代码:

package IfnotPos;

public class IfNotPositive extends ArithmeticException{

    public static void main(String[] args) throws ArithmeticException{

        IfNotPositive pos = new IfNotPositive();
        System.out.println(pos.addTwoPositive(-3,2));

    }

    public int addTwoPositive(int plus, int plus1) throws ArithmeticException{
        if(plus < 0 || plus1 < 0){
            throw new ArithmeticException("Non-positive integers sent!");

        }
        else{
            return plus + plus1;
        }

    }

    public ArithmeticException(String string) {
        return string;

    }

}

我收到“方法的返回类型丢失”的错误,如果我将其更改为字符串或其他任何内容,它显然将不再是构造函数。任何处理此异常的帮助将不胜感激。

【问题讨论】:

  • 谢谢大家。所以现在我不明白他们所说的“传递字符串‘发送的非正整数!’是什么意思。到异常的构造函数。”

标签: java exception constructor throw


【解决方案1】:

首先,您不需要扩展 ArithmeticException 类。您不是在创建自定义异常,而是编写程序以在不满足条件时抛出 ArithmeticException(注意它位于 java.lang 中)。

您的代码的问题是您定义的构造函数永远不会存在并且不正确,因此存在编译问题。

public ArithmeticException(String string) {
    return string;
}

只需删除这个构造函数,你就可以开始了。

【讨论】:

  • 所以现在我不明白他们所说的“传递字符串'发送的非正整数!'是什么意思!”到异常的构造函数。”
  • 它只是告诉你做什么的不同方式——你也可以这样理解——“抛出一个算术异常,消息'非正整数发送!'”
【解决方案2】:

来自Providing Constructors for Your Classes

构造函数声明看起来像方法声明——除了它们使用类的名称并且没有返回类型。

我收到“方法的返回类型丢失”的错误

您收到此错误是因为您的代码中有public ArithmeticException(String string) 行。

您不能在另一个类中添加任何其他类的构造函数,因此 Java 将 public ArithmeticException(String string) 视为函数而不是构造函数。

正如你所知道的,你需要有返回类型 mendetry 的方法


您不必扩展 ArithmeticException 类。见以下代码:

public class IfNotPositive{

    public static void main(String[] args) throws ArithmeticException{

        IfNotPositive pos = new IfNotPositive();
        System.out.println(pos.addTwoPositive(-3,2));

    }

    public int addTwoPositive(int plus, int plus1) throws ArithmeticException{
        if(plus < 0 || plus1 < 0){
            throw new ArithmeticException("Non-positive integers sent!");

        }
        else{
            return plus + plus1;
        }

    }
}

问:抛出一个 ArithmeticException,传递字符串“Non-positive integers sent!”

表示抛出ArithmeticException,并附上详细信息"Non-positive integers sent!"

因此,您可以使用 public ArithmeticException(String s) 构造函数。

构造带有指定详细消息的 ArithmeticException。

例如:

throw new ArithmeticException("Non-positive integers sent!");

【讨论】:

  • 所以现在我不明白他们所说的“传递字符串'发送的非正整数!'是什么意思!”到异常的构造函数。”
  • @userb 看到上面的答案。
【解决方案3】:

我不明白你为什么要在代码中为 ArithmeticException 创建构造函数?

    public ArithmeticException(String string) {
        return string;

    }

你不会写这样的构造函数。这是抱怨上述方法的语法异常。这个构造函数是不必要的。删除它,您的代码将完美运行。您应该看到下面的异常正在运行您的程序...

线程“主”java.lang.ArithmeticException 中的异常:非正数 整数发送!在 com.code.samples.IfNotPositive.addTwoPositive(IfNotPositive.java:14) 在 com.code.samples.IfNotPositive.main(IfNotPositive.java:8)

【讨论】:

  • 所以现在我不明白他们所说的“传递字符串'发送的非正整数!'是什么意思!”到异常的构造函数。”
猜你喜欢
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
相关资源
最近更新 更多