Jeff Schwab wrote:
I can't think of many applications where at() throwing an exception
would be in any way a desirable thing. (As mentioned, to catch
programming errors IMO using assert() is better.)
Huh? The exception is *exactly* the right solution, for the reasons red
floyd already explained.
Could you please explain to me why is that?
If indexing out of boundaries at some place is an indication of a
programming error, ie. a bug, the best course of action is to report
immediately about this detected error, at the place where it happens.
assert() is exactly the tool designed for this: It terminates the
program immediately, and with the aid of a debugger you can see the
whole stack trace and the values of the variables exactly when the error
happened. This way the programming error can be fixed, and the indexing
out of boundaries will not happen anymore.
I can't even begin to imagine why throwing an exception instead, and
catching it somewhere else than right where the error happens, would be
a better idea.