基本上,我正在尝试在Robocode中生成一个日志文件,但我遇到了一些问题,因为你不能在Robocode中使用try/catch (据我所知)。我做了以下工作:
public void onBattleEnded(BattleEndedEvent e) throws IOException
{
writeToLog();
throw new IOException();
}和
public void writeToLog() throws IOException
{
//Create a new RobocodeFileWriter.
RobocodeFileWriter fileWriter = new RobocodeFileWriter("./logs/test.txt");
for (String line : outputLog)
{
fileWriter.write(line);
fileWriter.write(System.getProperty("line.seperator"));
}
throw new IOException();
}并且我在编译时得到以下错误:-
MyRobot.java:123: onBattleEnded(robocode.BattleEndedEvent) in ma001jh.MyRobot cannot implement onBattleEnded(robocode.BattleEndedEvent) in robocode.robotinterfaces.IBasicEvents2; overridden method does not throw java.io.IOException
public void onBattleEnded(BattleEndedEvent e) throws IOException
^
1 error发布于 2011-03-05 22:17:59
正如您所看到的here,该接口没有声明任何检查过的异常。所以你不能在你的实现类中抛出一个。
解决这个问题的一种方法是像这样实现你的方法:
public void onBattleEnded(BattleEndedEvent e)
{
writeToLog();
throw new RuntimeException(new IOException());
}
public void writeToLog()
{
//Create a new RobocodeFileWriter.
RobocodeFileWriter fileWriter = new RobocodeFileWriter("./logs/test.txt");
for (String line : outputLog)
{
fileWriter.write(line);
fileWriter.write(System.getProperty("line.seperator"));
}
throw new new RuntimeException(new IOException());
}发布于 2011-03-06 10:53:10
但是我遇到了一些问题,因为你不能在Robocode中使用try/catch (据我所知)
这个假设是从哪里来的?我只是因为你的问题在这里安装了机器人代码(所以如果我以后不经常在这里回答是你的错),写了我自己的机器人,它可以很好地捕捉异常:
try {
int i = 1/0;
}
catch(ArithmeticException ex) {
ex.printStackTrace();
}为什么在你的例子中使用IOExceptions呢?
https://stackoverflow.com/questions/5204304
复制相似问题