Re: Job Interview, Did I Mess Up?
"Nevin :-] Liber" <nevin@eviloverlord.com> wrote:
"Daniel T." <daniel_t@earthlink.net> wrote:
In my experience (and it might be just in my domain,) if an
exception is thrown, then it is because a programmer made an
unwarranted assumption while writing some particular piece of code
and the code should be fixed so the exception is no longer thrown.
(In my problem domain, you are describing ASSERT, not exceptions.)
Several people have made comments like this one. I can imagine a domain
where just asserting and aborting the program, or allowing a bug that
wasn't found during test time to put the program in an undefined state,
would be a very bad thing; such systems would use exceptions to do some
sort of reset and problem logging. However, that isn't my domain.
I style my code after the standard when it comes to exceptions. A thrown
exception means that the code that detected the programmer error, is not
the code that needs to be fixed. (Precondition violations.)
Some who responded like to use exceptions for normal control flow that
is designed to deal with user errors. I personally don't think that is
appropriate.
I'm confused. In some of your examples, you use STL constructs (such
as vector and string), which can throw. How do you avoid things like
vector and string from throwing exceptions?
Good question. It's called being pro-active, for example:
I know that bad_alloc will not be thrown because I know how much memory
I have to work with, and I keep within that memory footprint. If
bac_alloc does get thrown, then that is a bug that must be fixed, not a
part of the normal control flow of the program.
I know that bad_cast will not be thrown because when I dynamic cast a
reference, I know it will succeed. If bad_cast does get thrown, then
that is a bug that must be fixed, not a part of the normal control flow
of the program.
I know that bad_typeid will not be thrown because when I get the typeid
of a dereferenced pointer, I know the pointer contains something. If
bad_typeid does get thrown, then that is a bug that must be fixed, not a
part of the normal control flow.
I could write a paragraph like the above for every single exception in
the standard (and every exception that my code throws,) but I'm sure you
get the idea.
(When working in a team, replace "I" with "We" in the above.)
Hopefully, this clears up my position on the issue.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]