Avoid unnecessary catching of Exception instead of StandardError.

Cherry-pick of c9891608aa1e5ebbab04afe73bf7a28748001804