What happened if CFile :: Write throws an exception?

Suppose a write operation throws an exception halfway through, is there any data written to the file or no data written to the file?

+1


source to share


2 answers


The short answer is that most likely some data will be written to the file, unless the disk is full at the start of the write operation.

Longer answer: This will depend on which CFileException is thrown from the call to Write.



http://msdn.microsoft.com/en-us/library/as5cs056(VS.80).aspx

0


source


Since you have no idea about the internals of a CFile (or not if it is properly encapsulated), you need to rely on an API "contract". In other words, unless the documentation states what happens in certain cases, you cannot rely on it.

Even if you have the source code and can understand it, the API specification is a contract, and anything not specified can change at any time. This is one of the reasons some software developers are wary of publishing internals, as then they can be locked to support this forever.



If you really want to make sure your file is in a known state after being thrown, you will need to code the behavior. It could be something like this:

  • file backup at program start (simple); or
  • backup before each save operation (still relatively simple); or
  • backup before any write operation (complex and slow).
0


source







All Articles