There's nothing about any particular iterator that announces, in some way, whether the iterator can be validly dereferenced. C++ is not a virtual machine-based code, like Java or C#, with the virtual machine keeping track of each object's validity.
It is possible that there might be compiler-specific options that enable additional run-time sanity checks. But since you haven't even identified your compiler, the answer here would simply be "check your compiler's documentation".
And if none of the options works for you, the answer will be "write it yourself". Based on a compile-time macro and by using certain coding conventions it would be possible, for example, to implement an iterator-compatible interface that performs additional sanity checks. For example, instead of declaring
std::set<int> set_of_ints;
and
std::set<int>::iterator b=set_of_ints.begin();
// Or something else that references std::set<int>::iterator
Declare and always use aliases:
typedef std::set<int> set_of_ints_t;
set_of_ints_t set_of_ints;
set_of_ints_t::iterator p=set_of_ints.begin();
... and so on. With this coding convention in place, it becomes easy to use a compile-time macro to enable additional sanity checks:
#ifndef DEBUG
typedef std::set<int> set_of_ints_t;
#else
typedef my_sanity_checked_set sets_of_ints_t;
#endif
With my_sanity_checked_set being a custom container that's interface-compatible with a std::set<int>, and with an iterator whose operators perform additional sanity checks on every operation (such as, for example, not incrementing or decrementing past the set's boundaries, dereferencing the end() value, etc...)
All this checking comes with extra overhead, of course. You would use this during development, then turn the whole thing off and compile using the native std::set for the release build. That's how it's done.
set.begin() == set.end()? It sounds like the real problem is a bonehead move on the coder; not the architects of the standard library..begin()should never throw an exception. And*s.begin()throwing exception doesn't have anything to do with.begin(). I edited the question.