Skip to main content
more structured
Source Link

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?"I believe you are asking two questions in one. It's a great questionShould you use exceptions or return values? Should you use RAII or not?

If exceptions are not permitted in your company, then fstream::exceptions() must be set globally for a flame waryour project. And you have to interrogate the error flag too.

If RAII is not permitted in your company, then do not use C++. If you use RAII, then any exception thrown in the destructor will be swallowed. This sounds terrible and it is. However I agree with others that RAII is the way to go also because any error handling here is futile and creates unreadable code. This is because "flush" does not do what you may think it does. It instructs the operating system to do it on your behalf. The explicit closeOS will do it when it believes it is convenient. Then, when the operating flushes (which may be a good idea if exceptions are deprecated byminute after your teamfunction returns) similar things may happen at the hardware level. The style anddisk may have an SSD cache which it flushes to the return value of you function actually suggests that thisrotating disks later (which may behappen at night when it is less busy). At last the casedata ends on the disk.

  But if exceptions are deprecated youthe story does not end here. The data may have to interrogatebeen saved correctly but the error flags after writing and after closing and return true or false accordinglydisk gets destroyed by whichever of the many possible causes. Just closing explicitlyHence if RAII is not transactionally safe enough by far.

fstream::exceptions() will tellfor you more or, then you set itneed to what you wantgo to a lower API level anyway and even that will not be perfect. Sorry for being a party pooper here.

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". It's a great question for a flame war.

The explicit close is a good idea if exceptions are deprecated by your team. The style and the return value of you function actually suggests that this may be the case.

  But if exceptions are deprecated you have to interrogate the error flags after writing and after closing and return true or false accordingly. Just closing explicitly is not enough by far.

fstream::exceptions() will tell you more or you set it to what you want.

I believe you are asking two questions in one. Should you use exceptions or return values? Should you use RAII or not?

If exceptions are not permitted in your company, then fstream::exceptions() must be set globally for your project. And you have to interrogate the error flag too.

If RAII is not permitted in your company, then do not use C++. If you use RAII, then any exception thrown in the destructor will be swallowed. This sounds terrible and it is. However I agree with others that RAII is the way to go also because any error handling here is futile and creates unreadable code. This is because "flush" does not do what you may think it does. It instructs the operating system to do it on your behalf. The OS will do it when it believes it is convenient. Then, when the operating flushes (which may be a minute after your function returns) similar things may happen at the hardware level. The disk may have an SSD cache which it flushes to the rotating disks later (which may happen at night when it is less busy). At last the data ends on the disk. But the story does not end here. The data may have been saved correctly but the disk gets destroyed by whichever of the many possible causes. Hence if RAII is not transactionally safe enough for you, then you need to go to a lower API level anyway and even that will not be perfect. Sorry for being a party pooper here.

added 1 character in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". ItsIt's a great question for a flamewareflame war.

The explicit close is a good idea if exceptions are deprecated by your team. The style and the return value of you function actually suggests that this may be the case.

But if exceptions are deprecated you have to interrogate the error flags after writing and after closing and return true or false accordingly. Just closing explicitly is not enough by far.

fstream::exceptions() will tell you more or you set it to what you want.

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". Its a great question for a flameware.

The explicit close is a good idea if exceptions are deprecated by your team. The style and the return value of you function actually suggests that this may be the case.

But if exceptions are deprecated you have to interrogate the error flags after writing and after closing and return true or false accordingly. Just closing explicitly is not enough by far.

fstream::exceptions() will tell you more or you set it to what you want.

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". It's a great question for a flame war.

The explicit close is a good idea if exceptions are deprecated by your team. The style and the return value of you function actually suggests that this may be the case.

But if exceptions are deprecated you have to interrogate the error flags after writing and after closing and return true or false accordingly. Just closing explicitly is not enough by far.

fstream::exceptions() will tell you more or you set it to what you want.

Just making my point at the beginning not the end
Source Link

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". Its a great question for a flameware.

The explicit close is a good idea if exceptions are deprecated by your team. The style and the return value of you function actually suggests that this may be the case.

But if exceptions are deprecated you have to interrogate the error flags after writing and after closing and return true or false accordingly. Just closing explicitly is not enough by far.

fstream::exceptions() will tell you more or you set it to what you want.

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". Its a great question for a flameware.

The explicit close is a good idea if exceptions are deprecated by your team. The style and the return value of you function actually suggests that this may be the case.

But if exceptions are deprecated you have to interrogate the error flags after writing and after closing and return true or false accordingly. Just closing explicitly is not enough by far.

fstream::exceptions() will tell you more or you set it to what you want.

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". Its a great question for a flameware.

At the end of the day this is a further incarnation of the famous question: "ARE EXCEPTIONS EVIL?". Its a great question for a flameware.

The explicit close is a good idea if exceptions are deprecated by your team. The style and the return value of you function actually suggests that this may be the case.

But if exceptions are deprecated you have to interrogate the error flags after writing and after closing and return true or false accordingly. Just closing explicitly is not enough by far.

fstream::exceptions() will tell you more or you set it to what you want.

clarification
Source Link
Loading
Source Link
Loading