Handle invalid indexes with a proper error, not a simple ValueError