[英]try catch exception block not working
My code jumps around a bit but what I want it to do is this: 我的代码跳了一下,但我想要它做的是:
when public void another day is run (in the hive class) the for loop iterates through an arraylist and gets the bees from it. 当公共无效的另一天运行时(在蜂巢类中),for循环遍历一个arraylist并从中获取蜜蜂。 It then tries using the bee.anotherDay() method on the bee which was in the arraylist.
然后它尝试在arraylist中的蜜蜂上使用bee.anotherDay()方法。
the anotherDay() method from bee runs eat() method from the bee class- adds 1 to the age of the bee and then every 3 days (starting on day 11) adds an egg to the arraylist in hive. 来自蜜蜂类的另一个()方法运行来自蜜蜂类的eat()方法 - 将蜜蜂的年龄加1,然后每3天(从第11天开始)向蜜蜂中的arraylist添加一个鸡蛋。
the eat method- if there is enough honey (2 or more units) will take the 2 honey and if the bees health is 2 or less- will increase it by 1. If there is not enough honey then the bees health is reduced by 1 and if the health reaches 0 an exception is thrown. 吃法 - 如果有足够的蜂蜜(2个或更多单位)将采取2蜂蜜,如果蜜蜂健康是2或更少 - 将增加1.如果没有足够的蜂蜜,那么蜜蜂的健康减少1如果健康状况达到0,则抛出异常。
the exception is caught in the anotherDay() method of the hive which removes the bee with health 0. 该例外被捕获在hive的anotherDay()方法中,该方法删除了生命值为0的蜜蜂。
By using println a few times I think what is actually happening is that my exception is being thrown and caught but the bee is not being removed from the arraylist. 通过使用println几次,我认为实际发生的事情是我的异常被抛出并被捕获但是蜜蜂没有被从arraylist中删除。
My code: 我的代码:
public void anotherDay(){ //anotherDay method in hive class
for(int i = 0;i<cells.size(); i++){
Bee bee = cells.get(i);
try{
bee = bee.anotherDay();
}catch(Exception e){
cells.remove(i);
}
cells.set(i, bee);
}
}
public Bee anotherDay() throws Exception{ //anotherDay mehtod in my Queen class (subclass of Bee}
eat();
age++;
if(age%3 == 2){
hive.addBee(new Egg());
}
return this;
}
public boolean eat() throws Exception{
if(hive.honey >= 2){
hive.takeHoney(2);
if(health == 3){
}else{
health= health++;
}
return true;
}else{
health = health -1;
if(health == 0){
throw new Exception();
}
return false;
}
}
First comment, this isn't really the correct use of an Exception
. 首先评论,这不是对
Exception
的正确使用。 You should return a status code, maybe an enum
. 你应该返回一个状态代码,也许是一个
enum
。
Second comment, never throw new Exception()
. 第二条评论, 永远不要
throw new Exception()
。 Create your own Exception
class, otherwise you deal with all exceptions that could be thrown ( NullPointerException
, ArrayIndexOutOfBoundsException
etc) in exactly the samne way. 创建您自己的
Exception
类,否则您将完全按照samne方式处理可能抛出的所有异常( NullPointerException
, ArrayIndexOutOfBoundsException
等)。 This is a bug waiting to happen. 这是一个等待发生的错误。
Now onto your problem. 现在你的问题。 You do remove the item at index
i
from your List
, but you always add it back in again with your call to set
. 您确实从
List
删除了索引i
处的项目,但是您始终通过调用set
将其重新添加回来。
Never remove items from a Collection
while looping. 循环时切勿从
Collection
删除项目。 In fact never loop over a collection by index unless you have to. 实际上,除非必须,否则永远不要通过索引循环遍历集合。
Use an Iterator
which has a remove
method. 使用具有
remove
方法的Iterator
。 This is the only (almost only) safe way to remove from a collection while looping. 这是在循环时从集合中删除的唯一(几乎唯一)安全方法。
final Iterator<Bee> myIter = cells.iterator();
while(myIter.hasNext()) {
final Bee bee = myIter.next();
try{
bee.anotherDay();
}catch(Exception e){
myIter.remove();
}
}
There is no need for you another day method to return this
and there is no need to replace the reference in your List
with exactly the same reference. 您不需要另外一天的方法来返回
this
方法,并且无需使用完全相同的引用替换List
的引用。
Looking at your eat
method, there is quite a bit of tidying that can happen; 看看你的
eat
,可以发生相当多的整理; I would recommend this: 我会推荐这个:
public boolean eat() throws Exception{
if(hive.honey >= 2){
hive.takeHoney(2);
if(health < 3){
health++;
}
return true;
}
health -= 1;
if(health == 0){
throw new BeeOutOfHealthException();
}
return false;
}
Ie many of your assignments are rather confused. 即你的许多作业相当困惑。 You don't need the
else
if you return
in the if
. 如果你在
if
return
, if
不需要else
。 Your nested if
is a little odd - having an empty statement is certainly code smell. 你的嵌套
if
有点奇怪 - 有一个空语句肯定是代码味道。
Further the logic of taking honey from the hive
should really be in the hive for takeHoney
should probably return a boolean
if that amount of honey cannot be taken. 进一步的服用蜂蜜从逻辑
hive
确实应该在蜂巢的takeHoney
也许应该返回一个boolean
,如果不能采取蜂蜜的量。 That then reduces your method to: 然后,您的方法减少到:
public boolean eat() throws Exception{
if(hive.takeHoney(2)){
if(health < 3){
health++;
}
return true;
}
health -= 1;
if(health == 0){
throw new BeeOutOfHealthException();
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.