Added try catch blocks to handle exceptions