0

I've been digging around the past couple hours on how to implement simple save/load functionality to a game i'm making.

The save data is being stored in a text file, during the game the player can save, storing the player & enemy positions, score etc.

I'm using StreamWriter & StreamReader for this and am having trouble deciding whether to implement this with a using statement or try-catch.

To save space i'll just list the Save methods as they're fundamentally the same as the load methods.

Using:

 static void SaveAccounts(string path)
      {

            using (StreamWriter writer = new StreamWriter(path))
            {
                writer.WriteLine(accountNumber);
                writer.WriteLine(name);
                writer.WriteLine(address);
                writer.WriteLine(balance);     
            }
      }

Try Catch

    public void Save(StreamWriter writer)
     {
        writer.WriteLine(accountNumber);
        writer.WriteLine(name);
        writer.WriteLine(address);
        writer.WriteLine(balance);
     }

    public void Save(string filename)
    {
       StreamWriter writer = null;
        try
        {
            writer= new StreamWriter(filename);
            Save(writer);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (writer!= null) writer.Close();
        }
    }

In this case i've used 'accounts' in place of my sprites, just seemed simpler.

So could anyone give me any insight into which you would use? If any.

Many thanks

Lee Brindley
  • 6,242
  • 5
  • 41
  • 62
  • 3
    Never write `throw e;`. http://stackoverflow.com/a/2999314/34397 – SLaks Apr 16 '13 at 00:48
  • Never write catch(Exception e) { throw e; } Resharper would remove the whole block for you (assuming you fix SLaks' point). – Aron Apr 16 '13 at 01:50

2 Answers2

3

You should use the using statement.

There is no reason to write 8 extra lines of code that do exact same thing, but wrongly.
There is also no reason to write 4 extra lines of code that do the same thing correctly.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks for your answer! If it isn't clear already, i'm a beginner - by using the using statement, what happens if I call Load on first startup of the program? As the path wouldn't exist? – Lee Brindley Apr 16 '13 at 00:53
  • @Fendorio: Exactly the same thing that would happen with your second example. IIRC, `new StreamWriter()` will create the file if it doesn't exist. – SLaks Apr 16 '13 at 03:49
1

I would use the try/catch even if it takes a few more lines. Why? Because with using you have no control of what happens when there is an exception. With try/catch you do.

As an example, imagine what happens if during the saving process there is an exception. With using the program just stops and closes the file however it is, with whatever information your program was able to write. That is, incomplete. With try/catch you can decide to delete the file. You could also write everything to a temp file and copy to the final location only if all code works fine, and if not just delete the temp file.

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
  • Thanks for taking time to answer! I agree I think I will go with the try-catch, until I find the way to deal with the incomplete data being saved. – Lee Brindley Apr 16 '13 at 01:14
  • @Fendorio You should only do this if you actually have exception handling in the catch block - above you only have `throw e;` (which should just be `throw;`) if you wish to rethrow after handling the error. You can also use both - using() to handle cleanup, surrounded by a try/catch to handle the exception. – TheEvilPenguin Apr 16 '13 at 01:26
  • @TheEvilPenguin In the catch i'll be telling the program to either create a new file (if filepath wasn't found), or in the case of Loading - tell it to just start the game from level 1. This would justify the try catch yes? – Lee Brindley Apr 16 '13 at 01:42
  • 1
    @Fendorio It would be better to check if the file exists (`File.Exists(path)`) and then handle things like permission errors in the catch block. Exceptions shouldn't be used for normal program flow as they're slow to create and your intent isn't clear when you use them like that. – TheEvilPenguin Apr 16 '13 at 02:58
  • Thanks again! You've been much help :) – Lee Brindley Apr 16 '13 at 03:05