Handle exceptions gracefully