refactored the try catch code for handling exception.