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