Error handling

As with all code, it is recommended that you handle errors. This can be performed using a try/catch block to catch exceptions, and display an appropriate message to the user when necessary.

For example:

System.IO.IOException

For further information of handing IO exceptions, see Microsoft’s guide:
https://learn.microsoft.com/en-us/dotnet/standard/io/handling-io-errors

Sharing violation
This occurs when another piece of software (such as anti-virus or cloud software) accesses the file while Easy Save is trying to access it. There is no way to prevent this from happening, so it is recommended that you inform the user.

Not enough disk space
If the user does not have enough storage left on their device, this exception will be thrown.

Hardware issue
The user’s hardware (such as hard drive) is damaged and unable to perform the IO operation.

System.UnauthorizedAccessException

Access to the path x is denied
This occurs when the user doesn’t have the required permissions to save or load to a specific file. Permissions are set by the operating system, so there’s no way to prevent this, so it is recommended that you tell the user.

System.FormatException

The data has been modified or corrupted so is no longer valid
This can occur when the user or an external piece of software tries to modify the file. For example if the user opened the file in a text editor and pressed save, it may try to apply a different encoding to the data which would corrupt it.

Anti-virus software can also cause this as it tries to scan the file between saves, stealing the file lock. If your file contains only whitespace then this is likely what has happened. In this case you can either tell your users to add an exception to their anti-virus for the file extension you are using, or in most cases enabling caching will resolve the issue as it doesn’t give the anti-virus the opportunity to steal the lock as it only uses a single write call.

Accessing encrypted data when encryption is disabled
This would be considered a coding error, as you should never put the user in a position where they can trigger this error.

The encryption password is incorrect
This can indicate that the encryption password does not match the one used to encrypt the data.

System.Security.SecurityException
System.Security.Cryptography.CryptographicException

The password is incorrect
This can be triggered if the save file you’re saving to or loading from is encrypted, and the password you’re using doesn’t match the one used to encrypt it.

The data has been modified or corrupted so is no longer valid
This can occur when the user or an external piece of software tries to modify the file. For example if the user opened the file in a text editor and pressed save, it may try to apply a different encoding to the data which would corrupt it. Anti-virus software can also cause this if it falsely thinks that your save file is malicious as it will usually replace the contents of the file with blank data.

System.ArgumentException

Accessing unencrypted data with encryption enabled
This would be considered a coding error, as you should never put the user in a position where they can trigger this error.

The data has been modified/corrupted and is no longer valid
This can occur when the user or an external piece of software tries to modify the file. For example if the user opened the file in a text editor and pressed save, it may try to apply a different encoding to the data which would corrupt it. Anti-virus software can also cause this if it falsely thinks that your save file is malicious as it will usually replace the contents of the file with blank data.