Note that there are some explanatory texts on larger screens.

plurals
  1. POtry catch exception block not working
    text
    copied!<p>My code jumps around a bit but what I want it to do is this: </p> <ol> <li><p>when public void another day is run (in the hive class) the for loop iterates through an arraylist and gets the bees from it. It then tries using the bee.anotherDay() method on the bee which was in the arraylist.</p></li> <li><p>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. </p></li> <li><p>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. </p></li> <li><p>the exception is caught in the anotherDay() method of the hive which removes the bee with health 0. </p></li> </ol> <p>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. </p> <p>My code:</p> <pre><code>public void anotherDay(){ //anotherDay method in hive class for(int i = 0;i&lt;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 &gt;= 2){ hive.takeHoney(2); if(health == 3){ }else{ health= health++; } return true; }else{ health = health -1; if(health == 0){ throw new Exception(); } return false; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload