Re: CFile and FILE*

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Wed, 26 Mar 2008 19:59:24 -0500, Joseph M. Newcomer
<newcomer@xxxxxxxxxxxx> wrote:

Never, ever call exit() in any C++ program, in any
Windows program, or, now that I stop to think of it, in any program, ever, for any reason.
Existing files will not be properly closed, for example, in any kind of program, including
console apps. There are too many things that can go wrong. I had made it a policy to
NEVER call exit() by 1980 or so (I was still faculty at CMU at the time, and I left in
early 1981). It is a lazy hack that avoids writing reliable and robust code.

FWIW, exit() flushes and closes files. There is a non-standard function
_exit which doesn't do this. In many console programs, it is perfectly fine
to use exit(exit_code) as the last statement of a fatal(exit_code, msg)
function. For example, I can't imagine why grep would want to avoid using
exit() in this way. In a more complex C++ console program, I can see
preferring exceptions to exit() and indeed I've done just that, but I hate
the fact that it's possible to swallow a FatalError exception. I really
wish there were a class of exceptions that could not be caught by
catch(...), could not be derived from anything, and which if caught
explicitly, force a rethrow at the end of the catch block.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: error handling
    ... >> Lisa Pearlson wrote: ... >>> Imagine I have a socket read function.. ... >>> exit 1 ... > exceptions to return data yet allow additional info.. ...
    (comp.lang.tcl)
  • Re: return in void functions
    ... Tripling the number of exit points per function almost ... >>triples the time it takes to debug a problem. ... > easier to use and works with multiple returns and exceptions. ... > really related to multiple exit points, ...
    (comp.lang.cpp)
  • Re: How do I terminate CWinApp application?
    ... A loop which is processing data which cannot exit cleanly is not ... This turns out to be clumsy in most cases, which is why C++ has exceptions. ... There is always a clean recovery. ... your processing function, when the message pump comes back to life. ...
    (microsoft.public.vc.mfc)
  • Re: Terminating program in ProcessWndProcException
    ... NO program should terminate except by user directive. ... One of our best programmers spent six months removing all but one exit() ... A more serious question is where are these exceptions coming from? ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: no return statement?
    ... You mentioned OCaml, but I'll write the code both in SML and OCaml (see ... the need to exit early does sometimes come up. ... You mentioned exceptions, which is a valid solution in both MLs. ... Here is an example of how to use the withEscape function: ...
    (comp.lang.functional)