Refactor try-catch block