Added error handling to the sample code.