Refactor try/except