2016-11-24 72 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在內存中創建一個新的引用,並且它不能等於已經存在的引用