Originally posted by WMCoolmon
You should take a look at the code for my ships.tbl reader I made awhile back.
Something I would call 'hackish' would not enforce format in any way, would just read the lines in and parse them. And it wouldn't have any globally defined constants; they'd all be tacked into the code.
I don't even know how to use exceptions in C++
I just (try to) keep them from happening (eg: Do not use a null pointer).
Although the whole exceptions thing strikes me as not having a place in a well-coded app, because generally you do want to keep that array from overflowing, or keep that null pointer from being used. Exceptions are (IMHO) like failing to avoid a fallen tree in the middle of the road because you have insurance.
I use exceptions as a sort of cascading fallback-on-error control; easiest way AFAIK to simply halt a program on error (although there's not really a recovery strategy that can be used here; just carry on and print out what result there is up to the failpoint)...
basically, if there's an error which can't be handled at the local 'level' (i.e. in the called routine), I can use an exception to inform the callee and let it either perform recovery/reaction, or simply pass that exception on the next callee layer, etc, until something that handles the exception receives it; what you're talking about is error avoidance, but exceptions are really for error detection and recovery anyways, which is a bit of an alternate paradigm - i.e. prevent the errors happening, but where there is the possibility of error, handle and recover (or fail gracefully) from it.
For example.... I'm working (well, was working) on a distributed application that, in simple terms, requires nodes to communicate with each other via specially formatted string messages. The nature of it is that each node sends messages to each other, and that each node is responsible for formatting the message prior to sending.
On receipt, the receiver uses a method to extract the information it needs; there's the possibility due to code error on the other node (perhaps a version difference, or a 3rd party coder - it's a general infrastructure being developed rather than a single app, or simply corruption) that the string message format is dodgy.
If so, I can simply throw an appropriate exception from the method that obtains info from the string, that will inform the receiver node (callee) of the formatting error; I can't handle the error itself within the method, because the callee needs to know there is a format problem which is by nature unrecoverable. I also need to skip
everything affected by it, hence the use of a try/catch block... point being, in this case I can't guarantee there is no error, because the data - the error source - comes from a 3rd party source and cannot be natively guaranteed as correct.
EDIT; not that I actually do error correction

(actually, it's a concept prototype; the deliberate intent is to fail-on-error at present. Not
my intent, though......)
That's an example for proper use of exceptions, IMO
