2016-11-24 73 views
0

我想抓到一个应该显示错误消息的异常,但似乎没有这样做。我做了另一个代码块,除了一些变量名称的变化之外完全一样,并且捕获了一个异常,但是这个代码似乎并没有这样做。例外的目的是该程序应寻找一个植物对象,如果没有找到,它应该抛出一个异常来表明食草动物只吃植物。下面是代码中的相关部分:为什么我没有正确捕捉到这个异常?

主要方法

import java.util.logging.Level; 
import java.util.logging.Logger; 

public class Main { 
    public Main() { 
     super(); 
    } 
      Rabbit rabbitExample = new Rabbit(); 


    public static void main(String[] args) { 

       Rabbit rabbitExample = new Rabbit();   

           System.out.println("************EXCEPTION 2************");   

     try {    

      Food pork = new Food("Prok"); 
      System.out.println("************Herbivore caught Exception example************"); 
      System.out.println("Exception caught"); 
      rabbitExample.eat(pork); 
      //wolfExample.eat(vegFood); 

     } catch (Exception e) { 
      // TODO: Add catch code 

      e.printStackTrace(); 
     } 

     try {  

      System.out.println("************Herbivore non-caught Exception example************"); 
      Food vegiFood = new Plant("Vegetables"); // you create a Meat object and store it in a Food variable (so to speak) 
      System.out.println("Herbivores eat " + rabbitExample.eat(vegiFood)); // must be surrounded by a try-catch block 
     } 
      catch (Exception ex) { 
      // TODO: Add catch code 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);; 

    } 
} 
} 

动物类

abstract public class Animal 
{ 

String name; 
int age; 
String noise; 

abstract public void makeNoise(); 

public String getName() { 
     return name; 
    } 

    public void setName(String newName) { 
     name = newName; 
    } 

abstract public Food eat(Food x) throws Exception; 

} 

兔类

public class Rabbit extends Herbivore 
{ 

    Rabbit() 
{ 
    name = "Haryy"; 
    age = 2; 
} 

    public void makeNoise() 
    { 
     noise = "Squeek!"; 
    } 
    public String getNoise() 
    { 
     return noise; 
    } 
    public String getName() 
    { 
     return name; 
    } 
    public int getAge() 
    { 
     return age; 
    } 

    public String eat(String Food) 
    { 
     return Food; 
    } 
    public Food eat(Food x) throws Exception 
    { 
     if (x.equals(new Plant("Meat"))) { 
       throw new Exception("Herbivores only eat plants!"); 
      } else { 
       return x; 
      } 
    } 
} 

草食类

public class Herbivore extends Animal 
{ 

    public Food eat(Food x) throws Exception 
    { 
     if (x.equals(new Plant("Meat"))) { 
       throw new Exception("Herbivores only eat plants!"); 
      } else { 
       return x; 
      } 

    } 

    public void makeNoise() 
    { 
     noise = "Woof!"; 
    } 
    public String getNoise() 
    { 
     return noise; 
    } 
} 

食品类

public class Food { 

    //field that stores the name of the food 
    public String name; 

    //constructor that takes the name of the food as an argument 
    public Food(String name){ 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 
    @Override 
    public String toString() { 
    return name; 
    } 
} 

我的辩护一下显示的代码量,但是我想表明,以运行该程序所需的所有相关的代码。作为显示输出:

************EXCEPTION 2************ 
************Herbivore caught Exception example************ 
Exception caught 
************Herbivore non-caught Exception example************ 
Herbivores eat Vegetables 

,而输出应该是:

************EXCEPTION 2************ 
************Herbivore caught Exception example************ 
Exception caught 
java.lang.Exception: Herbivores only eat plants! 
************Herbivore non-caught Exception example************ 
Herbivores eat Vegetables 

任何帮助表示赞赏,感谢。

+0

你的代码在这么多层次上感到困惑,我不知道从哪里开始。 (1)你为什么认为一种新食品(“Prok”)会等于一种“新植物”(“肉”)? (2)为什么你的代码在一个没有捕获异常的地方打印'Exception caught'? (这只发生在“catch”块中)。 (4)为什么你的代码在你尝试'eat'方法之前打印'Exception caught'? (3)你认为哪行代码会打印'java.lang.Exception:草食动物只吃植物!'? – ajb

回答

2

Hetre是你的问题:

public Food eat(Food x) throws Exception 
    { 
     if (x.equals(new Plant("Meat"))) { 
       throw new Exception("Herbivores only eat plants!"); 
      } else { 
       return x; 
      }  
    } 

你的食物子类没有覆盖equals()(和hashcode())。类Object中的默认实现愉快地进行==比较,即使它们在逻辑上相同,对于不同的对象也总是为false。

但抵制实施equals()(和hashcode())类Food的诱惑。只有具体的类(您创建对象的类)才能真正知道其他对象是否为等于

+0

啊,我看到了,我把它改成了'x instanceof Plant',它现在可以工作,谢谢。 – James

0

这个if (x.equals(new Plant("Meat")))将总是产生错误(除非x参数被传递为null),因此你的异常永远不会被触发,因为新的Plant在内存中创建一个新的引用,并且它不能等于已经存在的引用