Adding try catch blocks to  the code