Added exceptions to handle errors gracefully.