Cache class: Should Miss be an exception?
I've got a system that needs to fetch settings from a remote device over
a communications link. I cache these in a Cache class. When reading a
setting, I first see if it's in the cache. If not, I fetch it from the
external device and then cache the result.
Should a cache miss be implemented as an exception or an extra return
value? What are the relative merits of the two implementions?
My current implementation uses an exception, and so far the only drawback
is that it generates a bunch of debugger noise in MS Visual Studio. But I
don't think VS is being reasonable in reporting every exception without
the ability to silence it. Is gdb subject to acting like this?
Here's an example of how one might use the Cache class, using both an
exception and return code scheme for reporting cache misses:
#if USE_MISS_EXCEPTION
double getRegister(unsigned registerIndex)
{
try {
return settingsCache.getSetting(registerIndex);
} catch (SettingsCache::Miss&) {
return getRegisterUncached(registerIndex);
}
}
#else
double getRegister(unsigned registerIndex)
{
double value;
const bool miss = settingsCache.getSetting(registerIndex, &value);
if (miss)
value = getRegisterUncached(registerIndex);
return value;
}
#endif