PHP (or other): Strategy to deal with exceptions that “cannot occur”
问题 Consider the following code. class C {} /** * @throws \InvalidArgumentException */ function classCreateInstance($class) { if (!is_string($class)) { throw new \InvalidArgumentException("Class name must be a string."); } if (!class_exists($class)) { throw new \InvalidArgumentException("Class '$class' does not exist."); } return new $class(); } /** * @return C */ function foo() { return classCreateInstance(C::class); } There is one function that may throw an exception, because it does not know