added a try catch block to handle errors