Added try/except blocks to sample code.