Catch errors and handle exceptions