Move check for data object further up function. Throw exception on error