Re: using try/catch as control mechansim, is it a good thing ?
On Mar 5, 6:44 am, "mast...@yahoo.com" <mast...@yahoo.com> wrote:
The subject line was a bit misleading, you aren't using exceptions as a
control mechanism, which is a bad thing, you are using then to catch an
exceptional condition, which is a good thing.
Thank you Ian.
So even though the exceptional condition I catch is a syntax error in
the file which is getting parsed, this approach is still okay, is it ?
It is convenient for me because of course whenever an exception is
caught the memory is released poperly. So that's why i ended up using
it in that case.
I think it also depends no what you intend to do about it. If the
parser cannot continue or do anything useful then an exception is
probably fine. If it is going to try to complete the parse anyway (and
maybe try to flag other errors) then some other error handling
mechanism is probably more suitable.
Depending on what I'm parsing I may use either mechanism. If the text
to be parsed is generated by a user it is often better to try to
identify many errors at once (like your C++ compiler no doubt does).
If it is expected that the text is generated by a program then it is
entirely appropriate to stop on the first error (as XML parsers must).
Even if it would be convenient for your users to get more than one
error at a time this sometimes complicates the parser to such an
extent, and produces so many false errors that it may not be very
useful. You'll see this a lot in C++ parsers where the compiler can't
filter out secondary errors caused by an earlier problem.
I'd also say exceptions are fine for what you're doing. I use them
under these circumstances myself.
K