Improved error handling with exceptions