Refactor try..except out of serialize().