
On 09/04/2011 20:29, Artyom wrote:
line 646: Normally it's undefined behavior to dereference an iterator like this. I would prefer an assert to throwing an exception.
The point is that these locations are valid positions just you can't call operator*.
I think it is better behavior because it is quite easy to make a mistake with this.
(From my experience with this)
Of course, it's a good thing to detect the error. The thing is that an exception means that you unwind the stack, possibly handling the exception at a higher level. That's normally not the correct response for a programming error.
It is similar to std::vector's at()...
Maybe, in any case I had found exception helpful, especially in situation when you don't a error in some part to shutdown entire process.
Defensive but forgiving programming.
Definitely a no-no. Dereferencing an invalid iterator is a programming error, not an exceptional situation. The thing to use is an assertion. When your code is statically wrong, the compiler refuses to generate a binary. Likewise, when your code is dynamically wrong, it just terminates. Fix your code such that such situations may *never* happen. An exception is something that *could* happen, even in bug-free software, like a failure to perform an operation due to lack of the necessary resources.