Anyone who wrote that at any place where I've ever worked would
not be allowed to touch the code again. Exceptions are NOT a
flow control mechanism.
Any boss who think like that I would not want to work for.
Exception is certainly a flow control mechanism. Essentially it is a
glorified longjmp with smart stack unwinding that invokes destructors.
If it looks like a duck and quakes like a duck, then it is a duck.
Now of course, the question remains when is the proper use of such type
of flow control. Is it *always* only valid for error handling purposes
and nothing else? That I cannot say I have enough experience to
answer, but I find it hard to believe that there is no other uses of an
inter-stackframe branching mechanism that is aware of cleaning up
objects in those frames, beside error handling.
But I like to bring up one possible use:
In some of the algorithm books I've been reading, many of them have
terminating statements (like 'return') nested deeply in nested loops.
So, once a certain terminating condition is reached, the entire
algorithm is done and exited. If the entire algorithm has been
implemented in a single function, then the 'return' statement is
sufficient to exit the algorithm. But what if helper functions were
used and that the terminating conditions are inside these helper
functions? In this case, you can either have the helper functions
return a terminating return code or you can have the main function
setup a try-catch block and let the helper function throw an exception.
Now imagine what if helper functions can have helper functions too.
Don't you think throwing an exception here is the easiest way to return
control to the main function?
Of course, some people believe that 'return' statements should never be
in the middle of a function. A good implementation of an algorithm
would have every control flow reach the last statement before the
function-ending braces. If this is your philosophy, then to you
exception-handling should be used for error-purposes only.