我有一个Java任务,在这一点上我需要帮助。以下是要求:Java编程帮助 - 抛出和捕获异常
创建一个WindowMalfunction和PowerOut Events来模拟GreenhouseControls中可能出现的问题。该事件应在GreenhouseControls中适当地设置以下布尔变量:
windowok = false;
poweron = false;设置变量后,WindowMalfunction或PowerOut应该抛出一个异常来指定错误状态。为此创建一个扩展Exception的ControllerException类。
如果从WindowMalfunction或PowerOut引发异常,则Controller捕获异常,然后使用适当的消息启动紧急关闭。向Controller添加一个名为shutdown的方法,并在GreenhouseControls中重写此方法以完成关闭。
我已经创建了ControllerException类:
public class ControllerException extends Exception{
public ControllerException(String except){
super(except);
}
public String getMessage(){
return super.getMessage();
}
public void shutdown(){
}
}
现在我要实现它在GreenHouseControls类。这是我做了什么:
public class WindowMalfunction extends Event{
ControllerException newExcep= new ControllerException("Error:");
public WindowMalfunction(long delayTime) {
super(delayTime);
}
public void action() throws ControllerException {
}
}
目前,在WindowMalfunction的作用()方法,我需要真正扔我创建了ControllerException。然后,我需要在Controller.run方法中捕获异常。
public void run() throws ControllerException {
while(eventList.size() > 0)
// Make a copy so you're not modifying the list
// while you're selecting the elements in it:
for(Event e : new ArrayList<Event>(eventList)) {
if(e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
}
我该如何去做?
谢谢。