throw an exception for invalid $when values