In PHP, if a value is considered "unknown" (not per se invalid) should this raise a logic or runtime exception?
<?php
function foo($bar) {
// logic
if(!is_string($bar)) {
throw new \InvalidArgumentException('invalid *argument*');
}
if(strlen($bar) < 4) {
throw new \DomainException('invalid *bar*');
}
static $knownBars = array('bar1', 'bar2');
if(!in_array($knownBars)) {
throw new \DomainException('unknown *bar*');
//throw new \UnexpectedValueException('unknown *bar*');
}
// runtime
$bar;
}
The first 2 exceptions are obvious, however the last one remains a bit unclear to me. Both seem to make sense; a logic/domain error as we expect one of a defined data-set, a runtime/unexpected value error as we actually got a unexpected value.
Which one should i throw?
Also what if the logic part is a single setter method and we want to replace the static array (data-set) with a database lookup instead... Is it OK to expect runtime exceptions in logic code due database failure, etc? Or should we move the database lookup to the runtime code and still throw a logic exception if "bar" is considered unknown?