我想抓到一個應該顯示錯誤消息的異常,但似乎沒有這樣做。我做了另一個代碼塊,除了一些變量名稱的變化之外完全一樣,並且捕獲了一個異常,但是這個代碼似乎並沒有這樣做。例外的目的是該程序應尋找一個植物對象,如果沒有找到,它應該拋出一個異常來表明食草動物只吃植物。下面是代碼中的相關部分:爲什麼我沒有正確捕捉到這個異常?
主要方法
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
任何幫助表示讚賞,感謝。
你的代碼在這麼多層次上感到困惑,我不知道從哪裏開始。 (1)你爲什麼認爲一種新食品(「Prok」)會等於一種「新植物」(「肉」)? (2)爲什麼你的代碼在一個沒有捕獲異常的地方打印'Exception caught'? (這隻發生在「catch」塊中)。 (4)爲什麼你的代碼在你嘗試'eat'方法之前打印'Exception caught'? (3)你認爲哪行代碼會打印'java.lang.Exception:草食動物只吃植物!'? – ajb