Check for error first.

Check for error first before actually trying to get the home directory,
instead of after attempting the operation.