3

Basically, I am trying to generate a log file in Robocode, but I am having issues as you cannot use try/catch in Robocode (as far as I am aware). I have done the following:

public void onBattleEnded(BattleEndedEvent e) throws IOException
{
    writeToLog();
    throw new IOException();
}

and

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();
}

and am getting the following error at compile time:-

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
tshepang
  • 12,111
  • 21
  • 91
  • 136
Jack H
  • 2,440
  • 4
  • 40
  • 63

2 Answers2

2

As you can see here, the interface doesn't declare any checked exceptions. So you can't throw one in your implementing class.

One way to solve this would be to implement your method like this:

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());
}
adarshr
  • 61,315
  • 23
  • 138
  • 167
  • See therein is the problem; if I take it away, the compiler then gives an error saying basically that the Exception must be caught or declared. – Jack H Mar 05 '11 at 14:22
  • Then declare as `throws RuntimeException` – adarshr Mar 05 '11 at 14:23
  • Thanks, as you can probably tell I am relatively new to Java, and very new to Exception Handling. Anyway, so I declared as `throws RuntimeException`, and the compiler still wants me to catch or declare the `IOException`. I tried to use `throw new RuntimeException`, but that wouldn't work. Thanks. – Jack H Mar 05 '11 at 14:39
  • Can you please show me the contents of the interface? I feel I'm missing something important. – adarshr Mar 05 '11 at 14:40
  • Sorry, what exactly do you mean by that. Sorry, I am new to this whole Java thing. Thanks. – Jack H Mar 05 '11 at 14:50
  • I mean, is [this](http://robocode.sourceforge.net/docs/robocode/robocode/robotinterfaces/IBasicEvents2.html) the interface you have implemented in MyRobot.java? – adarshr Mar 05 '11 at 14:52
  • I have used the method in there yes, how would I "implement" it? Sorry, I know this must be tedious. – Jack H Mar 05 '11 at 14:59
1

but I am having issues as you cannot use try/catch in Robocode (as far as I am aware)

Where did this assumption came from? I just because of your question here installed robocode (so it's your fault if I'll answer here less often in future), wrote my own robot and it can catch exceptions quite good:

try {
   int i = 1/0;
}
catch(ArithmeticException ex) {
   ex.printStackTrace();
}

And why are you throwing IOExceptions in your example?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • Yeah, sorry I got a little confused with the limits robocode places with regards to exception handling. I tried that as part of a trial/error scenario. I am after all a new CS student, mistakes have to be made. – Jack H Mar 13 '11 at 15:16