Refactor the main method to catch the exceptions