Fix order of error checks.
Fixes #9042.