Update code to handle exceptions