Refactor try catch