Handling potential exceptions as suggested by @Eric89GXL's review.