Add exception handling to sample code