Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Java方法不会在测试类中引发异常?_Java_Exception - Fatal编程技术网

为什么Java方法不会在测试类中引发异常?

为什么Java方法不会在测试类中引发异常?,java,exception,Java,Exception,为什么我不能从哺乳动物对象中的takeDamage方法抛出异常,并在创建该对象的测试类中捕获它 package animals; public class Mammal extends Animal{ public final boolean predation, herbivory; public String hairColor; public Mammal() { super(); this.predat

为什么我不能从哺乳动物对象中的takeDamage方法抛出异常,并在创建该对象的测试类中捕获它

    package animals;
    public class Mammal extends Animal{
      public final boolean predation, herbivory;
      public String hairColor;

      public Mammal() {
        super();
        this.predation = rand.nextBoolean();
        this.herbivory = rand.nextBoolean();
        this.hairColor = colors[rand.nextInt(8)];
      }

      public Mammal(boolean predation, boolean herbivory, String hairColor) {
        super();
        this.predation = predation;
        this.herbivory = herbivory;
        this.hairColor = hairColor;
      }
      public Mammal(boolean predation, boolean herbivory, String hairColor, String name, boolean sex) {
        super(name, sex);
        this.predation = predation;
        this.herbivory = herbivory;
        this.hairColor = hairColor;
      }

      public void takeDamage(int damage) throws TakeDamageException{
        if(damage < 1) {
          throw new TakeDamageException("Damage cant be negative!");
        }
        else this.health -= damage;
      }
将我的异常类定义为:

public class TakeDamageException extends Exception {
  public TakeDamageException(String message)  {
    super(message);
  }
  public TakeDamageException() {
  }
}

您可能有一个潜在的问题:从静态上下文引用非静态方法
takeDamage(int)
。即使您在
哺乳动物
的子类中工作,也需要引用它的一个实例来使用该方法。

一个更简洁的示例就可以了。我不同意这个测试应该从哺乳动物身上继承,但我把它放在这里作为例子:

Madamal.java:

public class Mammal {
int health =500;
  public void takeDamage(int damage) throws Exception{
    if(damage < 1) {
      throw new Exception("Damage cant be negative!");
    }
    else this.health -= damage;
  }
}


你可能一次做的太多了。婴儿步骤。

从您显示的内容来看,这一行不会编译:takeDamage(-123);改为哺乳类。takeDamage(-123);它应该,你应该看到异常。Sry犯了一个错误,我把它编译成哺乳动物。takeDamage(-123);我身边有你的代码例外我犯了错误,我称之为哺乳动物。takeDamage(-123);非常感谢你。如果我尝试抛出自定义异常,您是否知道为什么它不起作用?
public class Mammal {
int health =500;
  public void takeDamage(int damage) throws Exception{
    if(damage < 1) {
      throw new Exception("Damage cant be negative!");
    }
    else this.health -= damage;
  }
public class MTest extends Mammal {
public static void main(String args[]) {
    Mammal m = new Mammal();
    try {
        m.takeDamage(-100);
    } catch(Exception e) {
        System.out.println("Exception");
    }
}