This site requires JavaScript, please enable it in your browser!
Greenfoot back
seelensauger
seelensauger wrote ...

2013/1/17

must be caught or declared to be thrown

1
2
seelensauger seelensauger

2013/1/17

#
ich bekomme die Fehlermeldung :"unreported excetion java-lang.Interrupted; must be caught or declared to be thrown" ich habe mir schon einige foren angeschaut aber ich verstehe die Fehlermeldung immer noch nicht ganz und erhoffe mir hilfe. Wie genau soll dieses try and catch funktionieren? mein ursprünglicher code:
public void pause()
    {
        
        for(Object obj : getWorld().getObjects(null))
        {
            Actor actor = (Actor) obj; 
            actor.wait();
        }
        
    }
er solle alle actors pausieren
seelensauger seelensauger

2013/1/17

#
oh it's an english forum i forgot: i get the failure: "unreported excetion java.lang.Interrupted; must be caught or declared to be thrown" I looked up in a few other forums, but I still don't understand the failure. I know it should funktion with try and catch but i have no idea how it exactly should funktion. the code, which should pause all the activities:
public void pause()
    {
        
        for(Object obj : getWorld().getObjects(null))
        {
            Actor actor = (Actor) obj; 
            actor.wait();
        }
        
    }
davmac davmac

2013/1/17

#
Do not call 'actor.wait()'. It doesn't do what you think it does.
seelensauger seelensauger

2013/1/17

#
what should i call then? what does is do then?
danpost danpost

2013/1/17

#
Please refer to the Greenfoot class API in the documentation for which methods might be of use.
seelensauger seelensauger

2013/1/17

#
it's not an ecspecialy greenfoot class it's the class object of Java http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html
danpost danpost

2013/1/17

#
seelensauger wrote...
what should i call then?
This is what I was answering.
davmac davmac

2013/1/17

#
what does is do then?
Roughly speaking, it halts the current thread (i.e. the whole simulation) until another thread calls 'notify' on the same object (which won't happen). If you just want to stop the whole simulation, call Greenfoot.stop(). There is no need to loop through the actors in this case.
seelensauger seelensauger

2013/1/17

#
i want add a new thread then which hasen't stopped then. This one will notify all the threads again when the pause is over. after stopping all the threads it still finishes the execution of the method which is running at the moment. (I tried this out with the greenfoot.stop() ) so in fact I want stop the whole simulation. @ danpost: I don't get it anymore
seelensauger seelensauger

2013/1/17

#
actually i the code is wrong, i think it has to be :
for(Object obj : getWorld().getObjects(null))
        {
            obj.wait();
        }
because wait() is not a methode of actor but of Object
davmac davmac

2013/1/17

#
i want add a new thread then which hasen't stopped then.
I really don't think that's what you want. How would another thread know that the pause was over?
This one will notify all the threads again when the pause is over.
There's only one thread, which all the actors act in. Calling 'wait' for each actor doesn't make sense. You really have two sensible options: use Greenfoot.stop(), or otherwise, have a variable in your world which the actors check before they do anything (which you can therefore use as a pause control).
davmac davmac

2013/1/17

#
actually i the code is wrong, i think it has to be :
No, that doesn't make any difference. Actor extends Object, so it inherits all the public methods including wait(). But, as I explain above, you don't want to call wait().
seelensauger seelensauger

2013/1/17

#
hmm I'd liked to find another way but it seems this is much easier. thanks much.
danpost danpost

2013/1/17

#
There is one other way to stop the world. That is to create and start a new world that is passed a reference to the old world. While the new world is active, the old world will be in a paused state. You can then return to the old world whenever a certain condition occurs.
// from first world
WaitWorld ww = new WaitWorld();
Greenfoot.setWorld(ww);
ww.setFirstWorld(this); // or ww.setFirstWorld(getWorld()) if not in world class
// in WaitWorld
// instance variable
private World firstWorld;
// method
public void setFirstWorld(World world)
{
    firstWorld=world;
}
// to return to first world
Greenfoot.setWorld(firstWorld);
The one drawback is you will not be able to view the first world while it is paused.
davmac davmac

2013/1/17

#
danpost, that's clever - I haven't come across that before.
There are more replies on the next page.
1
2