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

2
  • 3
    Never write throw e;. stackoverflow.com/a/2999314/34397 Commented Apr 16, 2013 at 0:48
  • Never write catch(Exception e) { throw e; } Resharper would remove the whole block for you (assuming you fix SLaks' point). Commented Apr 16, 2013 at 1:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
@Fendorio: Exactly the same thing that would happen with your second example. IIRC, new StreamWriter() will create the file if it doesn't exist.
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.

5 Comments

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.
@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 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?
@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.
Thanks again! You've been much help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.